How to check if a string is a integer ?

W

w wg

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

Stefan Rusterholz

w said:
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
 
F

Farrel Lifson

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

w wg

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.
 
B

Bertram Scharpf

Hi,

Am Montag, 04. Jun 2007, 22:46:45 +0900 schrieb Stefan Rusterholz:
w said:
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
 
D

David Mullet

Farrel said:
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
 
S

Stefan Rusterholz

Bertram said:
Hi,

Am Montag, 04. Jun 2007, 22:46:45 +0900 schrieb Stefan Rusterholz:
w said:
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
 
B

Bertram Scharpf

Hi,

Am Dienstag, 05. Jun 2007, 01:00:10 +0900 schrieb Stefan Rusterholz:
Bertram said:
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
 
P

Peña, Botp

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) =3D> 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) #=3D> 123
Integer("0x1a") #=3D> 26
Integer(Time.new) #=3D> 1049896590


same w qri kernel#integer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top