To check for numericality

Hi
  I have
  @val=SomeValue #for example 00,02,aa
  How can I check @val contains only numeriacal(including zero)
values..I have to avoid aa above (for example )

Sijo

···

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

Sijo Kg wrote:

Hi
  I have
  @val=SomeValue #for example 00,02,aa
  How can I check @val contains only numeriacal(including zero)
values..I have to avoid aa above (for example )

Sijo

data = ["00", "aa", "0a", "a0", "02"]

data.each do |str|
  if str =~ /\D/
    print "bad data: ", str
    puts
  end
end

--output:--
bad data: aa
bad data: 0a
bad data: a0

···

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

@val = someValue if ( Integer( someValue) rescue Float( someValue )
rescue false)

···

2008/5/8 Sijo Kg <sijo@maxxion.com>:

Hi
  I have
  @val=SomeValue #for example 00,02,aa
  How can I check @val contains only numeriacal(including zero)
values..I have to avoid aa above (for example )

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

Hi
    Thanks for your reply It worked.At first I tried like
if /^(\d)(\d)$/.match(@val) ==nil #But not worked..Anyway thanks

Sijo

···

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

Well that's two (or three) very useful things I didn't know.
(I have the first edition of Programming Ruby.)

I didn't know you could use "rescue" without "begin" and "end",
and I didn't know you could use "Integer" and "Float"
to test whether the *whole* of a string would convert to a number.

In this situation is there any advantage to testing for "Integer" first,
instead of just using
newval = someValue if ( Float( someValue ) rescue false )

···

On 5/8/08, Farrel Lifson <farrel.lifson@gmail.com> wrote:

@val = someValue if ( Integer( someValue) rescue Float( someValue )
rescue false)

2008/5/8 Sijo Kg <sijo@maxxion.com>:

> Hi
> I have
> @val=SomeValue #for example 00,02,aa
> How can I check @val contains only numeriacal(including zero)
> values..I have to avoid aa above (for example )
>
> Sijo
> --
> Posted via http://www.ruby-forum.com/\.
>
>

Farrel Lifson wrote:

@val = someValue if ( Integer( someValue) rescue Float( someValue )
rescue false)

How would you fix this so it would work for valid numbers such as "1,234.56"? I realize that the OP uses comma separators, but Excel would import the above number as "1,234.56" to a .csv file.

···

2008/5/8 Sijo Kg <sijo@maxxion.com>:

Hi
  I have
  @val=SomeValue #for example 00,02,aa
  How can I check @val contains only numeriacal(including zero)
values..I have to avoid aa above (for example )

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

that is a bug:

cfp:~ > ruby -e' p Integer( bug = nil ) '
0

simply use 'Float(value) rescue false'

a @ http://codeforpeople.com/

···

On May 8, 2008, at 2:17 AM, Farrel Lifson wrote:

@val = someValue if ( Integer( someValue) rescue Float( someValue )
rescue false)

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

Sure is :wink: you will know that it is an Integer. But I guess you do not
care, in that case no, just go for the Float() rescue idiom.
HTH
Robert

···

On Thu, May 8, 2008 at 10:43 AM, Colin Bartlett <colinb2r@googlemail.com> wrote:

In this situation is there any advantage to testing for "Integer" first,
instead of just using
newval = someValue if ( Float( someValue ) rescue false )

Michael W. Ryder wrote:

Farrel Lifson wrote:

@val = someValue if ( Integer( someValue) rescue Float( someValue )
rescue false)

How would you fix this so it would work for valid numbers such as
"1,234.56"? I realize that the OP uses comma separators, but Excel
would import the above number as "1,234.56" to a .csv file.

You just failed i18n. 1.234,56 is a Continental European way to express
that, and Excel, for example, is aware of that distinction (so should
the OS be). :wink:

And with that last thought: leverage the locale settings of the Os,
somehow? Maybe via OpenOffice or Ruby bindings into this (if such a
library is available)?

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

~ Well, it just seemed wrong to cheat on an ethics test. -- Calvin

newval = someValue if ( Float( someValue ) rescue false )

'Float' is a class, ins't it? So how can you do "Float(whatever)" as if
it were a method?

Now, when I do "Kernel.methods" I _do_ see "Float", "Integer" etc in the
list. Is it the method "Kernel.Float" that eventually gets called? How
does ruby know to distinguish between the class Float and the method
Kernel.Float?

BTW, why, when I do "Object.methods" don't I see "Float", "Integer" etc
in the list? Some more methods too are missing. Object is supposed to
inherit them from Kernel, doesn't it?

···

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

ts wrote:

Albert Schlef wrote:
> How does ruby know to distinguish between the class Float and the method
> Kernel.Float?

Because you use it like a method, i.e. you have added () after it

Float() ==> method
Float ==> constant

I see.

> when I do "Object.methods" don't I see "Float", "Integer" etc
> in the list [...] Object is supposed to inherit them from Kernel,
> doesn't it?

#Float is a global function, i.e. a Kernel private method
[...]

Thanks Guy.

···

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