How to check string is numeric or not

Hi friends
I want to check the value is numeric or not
For example
when i am checking numeric or not am expecting as below results
"34" => true
34 => true
"34sdfds" => it should return false
34.90 => false

am using ruby 1.8.7. can anyone suggest isthere is anymethod exist to
check these

···

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

def is_numeric(o)
    true if Integer(o) rescue false
  end

···

On Thu, 12 Jul 2012 19:16:08 +0900 Lucky Nl <lists@ruby-forum.com> wrote:

Hi friends
I want to check the value is numeric or not
For example
when i am checking numeric or not am expecting as below results
"34" => true
34 => true
"34sdfds" => it should return false
34.90 => false

am using ruby 1.8.7. can anyone suggest isthere is anymethod exist to
check these

--
Sincerely yours,
Aleksey V. Zapparov A.K.A. ixti
FSF Member #7118
Mobile Phone: +34 677 990 688
Homepage: http://www.ixti.net
JID: zapparov@jabber.ru

*Origin: Happy Hacking!

I want to check the value is numeric or not

According to your example you want to check whether a string
represents an integer value (floats are numeric, too).

For example
when i am checking numeric or not am expecting as below results
"34" => true
34 => true
"34sdfds" => it should return false
34.90 => false

You can use Integer() to do that:

Ruby version 1.8.7
irb(main):001:0> %w{34 34sdfds 34.90}.each do |s| printf "%p: %p\n",
s, (Integer(s) rescue false) end
"34": 34
"34sdfds": false
"34.90": false
=> ["34", "34sdfds", "34.90"]

But actually you do not need a separate check, just use Integer to
convert the string to a Fixnum / Bignum and deal with the exception.

Kind regards

robert

···

On Thu, Jul 12, 2012 at 12:16 PM, Lucky Nl <lists@ruby-forum.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Aleksey Zapparov wrote in post #1068416:

check these

def is_numeric(o)
    true if Integer(o) rescue false
  end

This may not be exactly what is wanted:

Integer("0x123")

=> 291

If you just want to allow digits and nothing else, use a regexp:
/\A\d+\z/ =~ o

Also, note that "is_numeric" isn't an ideal name, because Numeric
includes Float.

Float.ancestors

=> [Float, Precision, Numeric, Comparable, Object, Kernel]

···

On Thu, 12 Jul 2012 19:16:08 +0900 > Lucky Nl <lists@ruby-forum.com> wrote:

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

or

/\A\d+\z/ === o

which returns true or false (instead of 0 or nil)

···

Am 12.07.2012 21:46, schrieb Brian Candler:

If you just want to allow digits and nothing else, use a regexp:
/\A\d+\z/ =~ o

--
<https://github.com/stomar/&gt;

If o is allowed to be a Fixnum, you need to cast to string first:

   /\A\d+\z/ === o.to_s

Alternatively

   !!o.to_s[/\A\d+\z/]

···

On 07/12/2012 10:30 PM, sto.mar@web.de wrote:

Am 12.07.2012 21:46, schrieb Brian Candler:

If you just want to allow digits and nothing else, use a regexp:
/\A\d+\z/ =~ o

or

/\A\d+\z/ === o

which returns true or false (instead of 0 or nil)

--
Lars Haugseth

WTF? (That's the first time I remember me writing this here.)

Let me get that straight: you want to convert something which you
*know* is an integer to a String to check whether it is an integer?

If o is allowed to be an integer type then you would better check that
directly (e.g. with Integer === o). But it get's better:

irb(main):010:0> a=[1,1<<50,"1"]
=> [1, 1125899906842624, "1"]
irb(main):011:0> a.each {|x| y=Integer(x); printf "type %p converted
%p type %p\n", x.class, y, y.class}
type Fixnum converted 1 type Fixnum
type Bignum converted 1125899906842624 type Bignum
type String converted 1 type Fixnum
=> [1, 1125899906842624, "1"]

Cheers

robert

···

On Fri, Jul 13, 2012 at 10:19 AM, Lars Haugseth <ruby-talk@larshaugseth.com> wrote:

If o is allowed to be a Fixnum, you need to cast to string first:

  /\A\d+\z/ === o.to_s

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Not quite. The question was about checking a value that could be a string, integer, float or presumably anything, and return true if the value contains digits only, false otherwise. At least that's how I interpret the message starting this thread.

···

On 07/13/2012 11:08 AM, Robert Klemme wrote:

On Fri, Jul 13, 2012 at 10:19 AM, Lars Haugseth > <ruby-talk@larshaugseth.com> wrote:

If o is allowed to be a Fixnum, you need to cast to string first:

   /\A\d+\z/ === o.to_s

WTF? (That's the first time I remember me writing this here.)

Let me get that straight: you want to convert something which you
*know* is an integer to a String to check whether it is an integer?

--
Lars Haugseth

seems the original post is not very clear,
the subject line implies only strings, the post itself does not.

···

Am 13.07.2012 11:41, schrieb Lars Haugseth:

On 07/13/2012 11:08 AM, Robert Klemme wrote:
Not quite. The question was about checking a value that could be a
string, integer, float or presumably anything, and return true if the
value contains digits only, false otherwise. At least that's how I
interpret the message starting this thread.

--
<https://github.com/stomar/&gt;