[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

to check for numericality

Sijo Kg

5/8/2008 7:01:00 AM

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

11 Answers

7stud --

5/8/2008 8:15:00 AM

0

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

Farrel Lifson

5/8/2008 8:17:00 AM

0

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

Sijo Kg

5/8/2008 8:34:00 AM

0

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

Colin Bartlett

5/8/2008 8:43:00 AM

0

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

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

Robert Dober

5/8/2008 12:07:00 PM

0

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

Albert Schlef

5/8/2008 1:18:00 PM

0

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

ts

5/8/2008 1:59:00 PM

0

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



Albert Schlef

5/8/2008 2:48:00 PM

0

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

Michael W. Ryder

5/8/2008 6:59:00 PM

0

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

ara.t.howard

5/8/2008 7:05:00 PM

0


On May 8, 2008, at 2:17 AM, Farrel Lifson 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'



a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama