#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Checking the type of class


    Hi, I'm new to Ruby and new to this forum, so apologies if I'm going against normal forum proceadures in my post (I have read the 'read this first' bit)

    Anyway I'm playing around with methods. I want to be able to check the class of a variable and make a decision on the result. When I test my method in irb it works but when I run it in a file it takes no effect. Here is what I tested in irb mode......

    five = 5
    five.class.eql?(Fixnum)
    return true

    Here's my method in the file......

    Code:
    def check_price?(pricex)
     if  pricex.class.eql? (Fixnum)
     then error("wrong type")
    else true end
    end
    If I type in a fixnum into the command line it fails the condition test.

    The output does not give me "wrong type"

    Any ideas? Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    My guess is that you are reading a string from the command line and not converting it first. Verify by printing out the actual class type in your function. Something like:
    Code:
    puts pricex.class
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0
    Originally Posted by L7Sqr
    My guess is that you are reading a string from the command line and not converting it first. Verify by printing out the actual class type in your function. Something like:
    Code:
    puts pricex.class

    Thanks, that did the job

IMN logo majestic logo threadwatch logo seochat tools logo