to check for numericality

S

Sijo Kg

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
 
7

7stud --

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

Farrel Lifson

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

Sijo Kg

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

Sijo
 
C

Colin Bartlett

[Note: parts of this message were removed to make it a legal post.]

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

2008/5/8 Sijo Kg said:
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
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 )
 
R

Robert Dober

In this situation is there any advantage to testing for "Integer" first,
instead of just using
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
 
A

Albert Schlef

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?
 
T

ts

Albert said:
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
 
A

Albert Schlef

ts said:
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.
 
M

Michael W. Ryder

Farrel said:
@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.

 
P

Phillip Gawlowski

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

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


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

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top