Comparison error

Discussion in 'Ruby' started by Jake Alucard, Oct 20, 2010.

  1. Jake Alucard

    Jake Alucard Guest

    so i'm writing a program for class that tells the user if the person is
    20 or under then they get a green and if the person is 21 or over then
    tehy get red here's the code and error




    age = ageText.to_s

    if age <= 20
    status = "GREEN"
    else
    status = "RED"
    end






    C:/ruby/martinilounge.rb:69:in `<=': comparison of String with 18 failed
    from C:/ruby/martinilounge.rb:69
    from C:/ruby/martinilounge.rb:98:in `call'
    from C:/ruby/martinilounge.rb:98:in `main'
    from C:/ruby/martinilounge.rb:98

    --
    Posted via http://www.ruby-forum.com/.
    Jake Alucard, Oct 20, 2010
    #1
    1. Advertising

  2. On 10-10-19 08:29 PM, Jake Alucard wrote:
    > so i'm writing a program for class that tells the user if the person is
    > 20 or under then they get a green and if the person is 21 or over then
    > tehy get red here's the code and error
    >
    >
    >
    >
    > age = ageText.to_s
    >
    > if age<= 20
    > status = "GREEN"
    > else
    > status = "RED"
    > end
    >
    >
    >
    >
    >
    >
    > C:/ruby/martinilounge.rb:69:in `<=': comparison of String with 18 failed
    > from C:/ruby/martinilounge.rb:69
    > from C:/ruby/martinilounge.rb:98:in `call'
    > from C:/ruby/martinilounge.rb:98:in `main'
    > from C:/ruby/martinilounge.rb:98
    >


    20 is not a string =), possible you want to do the following

    age = ageText.to_i

    you can also do

    age = Integer(ageText)

    the 2nd form will throw an ArgumentError exception if ageText is not a
    number, while the 1st form will simply return some integer value, you
    can play around with the them in irb:


    irb(main):001:0> "$55".to_i
    => 0

    irb(main):002:0> "33".to_i
    => 33

    irb(main):003:0> "33rr12".to_i
    => 33

    irb(main):004:0> nil.to_i
    => 0

    irb(main):005:0> "rrr".to_i
    => 0

    irb(main):006:0> Integer("12")
    => 12

    irb(main):007:0> Integer("$12")
    ArgumentError: invalid value for Integer(): "$12"
    from (irb):7:in `Integer'
    from (irb):7
    from /usr/local/bin/irb:12:in `<main>'



    --
    Kind Regards,
    Rajinder Yadav | DevMentor.org | Do Good! ~ Share Freely

    GNU/Linux: 2.6.35-22-generic
    Kubuntu x86_64 10.10 | KDE 4.5.1
    Ruby 1.9.2p0 | Rails 3.0.1
    Rajinder Yadav, Oct 20, 2010
    #2
    1. Advertising

  3. On Wed, Oct 20, 2010 at 7:23 AM, Brian Candler <> wrote:
    > Rajinder Yadav wrote in post #955622:
    >> you can also do
    >>
    >> age = Integer(ageText)
    >>
    >> the 2nd form will throw an ArgumentError exception if ageText is not a
    >> number

    >
    > Beware that it also has some other behaviour you might not expect.
    >
    >>> Integer("20")

    > => 20
    >>> Integer("00020")

    > => 16
    >>> Integer("0x00020")

    > => 32
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    nice thanks for pointing that out =)

    --
    Kind Regards,
    Rajinder Yadav | http://DevMentor.org | Do Good! - Share Freely
    Rajinder Yadav, Oct 20, 2010
    #3
    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. PW

    Number comparison error

    PW, May 23, 2004, in forum: ASP General
    Replies:
    7
    Views:
    185
    Roland Hall
    May 25, 2004
  2. Bill Cunningham

    comparison error

    Bill Cunningham, Oct 28, 2011, in forum: C Programming
    Replies:
    12
    Views:
    886
    Bill Cunningham
    Oct 30, 2011
  3. Greg Willits

    Error in Ruby text comparison?

    Greg Willits, Oct 27, 2007, in forum: Ruby
    Replies:
    6
    Views:
    134
    Devi Web Development
    Oct 27, 2007
  4. Deepu
    Replies:
    1
    Views:
    236
    ccc31807
    Feb 7, 2011
  5. bjjnova
    Replies:
    1
    Views:
    96
    John G Harris
    Apr 25, 2006
Loading...

Share This Page