Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Ruby Programming Language

How to check if a string is a integer ?


How to check if a string is a integer ?

After seached in google, I can't find any information. All ISINTEGER
function is about Javascript.

Thank you.

--
WenGe Wang

w wg wrote:
> How to check if a string is a integer ?

> After seached in google, I can't find any information. All ISINTEGER
> function is about Javascript.

> Thank you.

You can use Integer(), it raises if the String passed is not an integer.
In case Integer() confuses you: that's Kernel#Integer(), a method, not
the class Integer.
Also you can use a regex, e.g. str =~ /\A[+-]?\d+\z/

Regards
Stefan

--
Posted via http://www.ruby-forum.com/.

On 04/06/07, w wg <duzu@gmail.com> wrote:

> How to check if a string is a integer ?

You can use either the String#to_i or Kernel#Integer methods

irb(main):001:0> "1234".to_i
=> 1234
irb(main):002:0> "123A".to_i
=> 123
irb(main):003:0> "A123".to_i
=> 0
irb(main):004:0> Integer("A1234")
ArgumentError: invalid value for Integer: "A1234"
        from (irb):4:in `Integer'
        from (irb):4
irb(main):005:0> Integer("1234A")
ArgumentError: invalid value for Integer: "1234A"
        from (irb):5:in `Integer'
        from (irb):5
irb(main):006:0> Integer("1234")
=> 1234
irb(main):007:0>

Farrel

w wg wrote:
> How to check if a string is a integer ?

> After seached in google, I can't find any information. All ISINTEGER
> function is about Javascript.

> Thank you.

Call the is_a? (or kind_of?) method on the object:

123.is_a? Integer
=> true
'David'.is_a? Integer
=> false

David

http://rubyonwindows.blogspot.com

--
Posted via http://www.ruby-forum.com/.

Thank you, it works.

How did you find the Integer method of class Kernel ?

 I typed "ri Kernel", but I can't find any Kernel's class method.

2007/6/4, Farrel Lifson <farrel.lif@gmail.com>:

--
--
WenGe Wang
On 04/06/07, David Mullet <david.mul@gmail.com> wrote:

> w wg wrote:
> 123.is_a? Integer
> => true
> 'David'.is_a? Integer
> => false

That won't work

irb(main):001:0> "123".is_a? Integer
=> false

Farrel

Hi,

Am Montag, 04. Jun 2007, 22:46:45 +0900 schrieb Stefan Rusterholz:

> w wg wrote:
> > How to check if a string is a integer ?

> Also you can use a regex, e.g. str =~ /\A[+-]?\d+\z/

In many cases it's useful to test for hex representations as
well. Something like:

class String
  def as_uint
    case self
      when /\A0x[0-9a-f]+\z/i   then hex
      when /\A0b[0-1]+\z/       then bin
      when /\A0[0-7]+\z/        then oct
      when /\A(?:0|[1-9]\d*)\z/ then to_i
    end
  end
end

There should be a combination of Kernel#eval and $SAFE, too.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

Farrel Lifson wrote:
> On 04/06/07, David Mullet <david.mul@gmail.com> wrote:
>> w wg wrote:
>> 123.is_a? Integer
>> => true
>> 'David'.is_a? Integer
>> => false

> That won't work

> irb(main):001:0> "123".is_a? Integer
> => false

> Farrel

Of course not. Duh! My apologies.

Perhaps I shouldn't post before my first cup of tea.

David

--
Posted via http://www.ruby-forum.com/.

Bertram Scharpf wrote:
> Hi,

> Am Montag, 04. Jun 2007, 22:46:45 +0900 schrieb Stefan Rusterholz:
>> w wg wrote:
>> > How to check if a string is a integer ?

>> Also you can use a regex, e.g. str =~ /\A[+-]?\d+\z/

> In many cases it's useful to test for hex representations as
> well. Something like:

In those cases I'd even more use Integer(). It accepts about all
representations of Integers ruby itself understands. It is probably much
faster than a case/when, you also will have the value converted to an
integer afterwards, which you most probably want anyway.

Regards
Stefan

--
Posted via http://www.ruby-forum.com/.

Hi,

Am Dienstag, 05. Jun 2007, 01:00:10 +0900 schrieb Stefan Rusterholz:

> Bertram Scharpf wrote:
> > Am Montag, 04. Jun 2007, 22:46:45 +0900 schrieb Stefan Rusterholz:
> >> w wg wrote:
> >> > How to check if a string is a integer ?

> >> Also you can use a regex, e.g. str =~ /\A[+-]?\d+\z/

> > In many cases it's useful to test for hex representations as
> > well. Something like:

> In those cases I'd even more use Integer(). It accepts about all
> representations of Integers ruby itself understands.

Aah! I did not know that. Cool. Thanks.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

From: w wg [mailto:duzu@gmail.com] :
#  I typed "ri Kernel", but I can't find any Kernel's class method.

C:\family\ruby>qri integer
--------------------------------------------------------- Kernel#Integer
     Integer(arg)    => integer
------------------------------------------------------------------------
     Converts _arg_ to a +Fixnum+ or +Bignum+. Numeric types are
     converted directly (with floating point numbers being truncated).
     If _arg_ is a +String+, leading radix indicators (+0+, +0b+, and
     +0x+) are honored. Others are converted using +to_int+ and +to_i+.
     This behavior is different from that of +String#to_i+.

        Integer(123.999)    #=> 123
        Integer("0x1a")     #=> 26
        Integer(Time.new)   #=> 1049896590

same w qri kernel#integer

 
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc