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.

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

On 04/06/07, w wg [email protected] 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

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 L. [email protected]:

On 04/06/07, David M. [email protected] 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 R.:

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 S. wrote:

Hi,

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

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

Farrel L. wrote:

On 04/06/07, David M. [email protected] 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

From: w wg [mailto:[email protected]] :

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

Hi,

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

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