# 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

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/
puts
end
end

–output:–

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

2008/5/8 Sijo Kg [email protected]:

Hi
if /^(\d)(\d)\$/.match(@val) ==nil #But not worked…Anyway thanks

Sijo

On 5/8/08, Farrel L. [email protected] wrote:

values…I have to avoid aa above (for example )

## 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,
newval = someValue if ( Float( someValue ) rescue false )

On Thu, May 8, 2008 at 10:43 AM, Colin B.
[email protected] wrote:

In this situation is there any advantage to testing for “Integer” first,
newval = someValue if ( Float( someValue ) rescue false )

Sure is 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

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?

ts wrote:

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

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

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

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

that is a bug:

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

simply use ‘Float(value) rescue false’

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Michael W. Ryder wrote:
| Farrel L. 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).

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 G.
Blog: http://justarubyist.blogspot.com

~ Well, it just seemed wrong to cheat on an ethics test. – Calvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgjWm0ACgkQbtAgaoJTgL9lRgCdEWEeURvczOdkFjluzwUxmQiN
BhMAoIlqJSJ/21OIb0oLZh5vPyApP/Cz
=oO45
-----END PGP SIGNATURE-----

Albert S. wrote:

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?

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

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

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?

#Float is a global function, i.e. a Kernel private method and a
Kernel singleton method

vgs% ruby -e ‘p Kernel.singleton_methods.include?(“Float”)’
true
vgs%

vgs% ruby -e ‘p Kernel.private_instance_methods.include?(“Float”)’
true
vgs%

vgs% ruby -e ‘p Object.private_methods.include?(“Float”)’
true
vgs%

Guy Decoux

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.