Numeric boolean check

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

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 [email protected]
KOREA

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

| Privacy Policy | Terms of Service | Remote Ruby Jobs