[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to check if a string is a integer ?

w wg

6/4/2007 1:38:00 PM

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

10 Answers

Stefan Rusterholz

6/4/2007 1:47:00 PM

0

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

Farrel Lifson

6/4/2007 1:48:00 PM

0

On 04/06/07, w wg <duzuike@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

David Mullet

6/4/2007 1:56:00 PM

0

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

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

w wg

6/4/2007 2:02:00 PM

0

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.lifson@gmail.com>:
> On 04/06/07, w wg <duzuike@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
>
>


--
--
WenGe Wang

Farrel Lifson

6/4/2007 2:05:00 PM

0

On 04/06/07, David Mullet <david.mullet@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

Bertram Scharpf

6/4/2007 2:39:00 PM

0

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

David Mullet

6/4/2007 2:50:00 PM

0

Farrel Lifson wrote:
> On 04/06/07, David Mullet <david.mullet@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-....

Stefan Rusterholz

6/4/2007 4:00:00 PM

0

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

Bertram Scharpf

6/4/2007 4:05:00 PM

0

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

Peña, Botp

6/5/2007 1:38:00 AM

0

From: w wg [mailto:duzuike@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