numeric boolean check

Discussion in 'Ruby' started by Jason Lillywhite, Mar 17, 2009.

  1. 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Jason Lillywhite, Mar 17, 2009
    #1
    1. Advertising


  2. > 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
    KOREA
     
    Saji N. Hameed, Mar 17, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vighnesh
    Replies:
    3
    Views:
    1,033
    Paul Clement
    Aug 4, 2005
  2. Replies:
    5
    Views:
    955
    X-Centric
    Jun 30, 2005
  3. J Leonard
    Replies:
    4
    Views:
    12,763
    Mark Space
    Jan 19, 2008
  4. vighnesh
    Replies:
    3
    Views:
    191
    Paul Clement
    Aug 4, 2005
  5. Metre Meter
    Replies:
    7
    Views:
    404
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page