Forum: Ruby numeric boolean check

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A74096209c78118ff69d50a51129d697?d=identicon&s=25 Jason Lillywhite (jlillywh)
on 2009-03-17 05:46
Is there a way to check for numeric in Ruby similar to the way you can
in Lisp?

(number? 5)      => #t
(number? -0.453) => #t
(number? 'a)     => #f

returns true for any numeric value and false for everything else.

This:
Float(x) rescue false

almost works but doesn't return true on numeric values
621080472679266b8e9b81aff4800398?d=identicon&s=25 Saji N. Hameed (Guest)
on 2009-03-17 06:01
(Received via mailing list)
> Is there a way to check for numeric in Ruby similar to the way you can
> in Lisp?
>
> (number? 5)      => #t
> (number? -0.453) => #t
> (number? 'a)     => #f

irb(main):021:0> (10/3.0).is_a? Numeric
=> true
irb(main):022:0> (10/3).is_a? Numeric
=> true
irb(main):023:0> "a".is_a? Numeric
=> false


saji
--
--
Saji N. Hameed

APEC Climate Center                  +82 51 668 7470
National Pension Corporation Busan Building 12F
Yeonsan 2-dong, Yeonje-gu, BUSAN 611705      saji@apcc21.net
KOREA
This topic is locked and can not be replied to.