numeric boolean check

J

Jason Lillywhite

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
 
S

Saji N. Hameed

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 (e-mail address removed)
KOREA
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top