Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Comparison error

Reply
Thread Tools

Comparison error

 
 
Jake Alucard
Guest
Posts: n/a
 
      10-20-2010
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/.

 
Reply With Quote
 
 
 
 
Rajinder Yadav
Guest
Posts: n/a
 
      10-20-2010
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

 
Reply With Quote
 
 
 
 
Rajinder Yadav
Guest
Posts: n/a
 
      10-20-2010
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
comparison error Bill Cunningham C Programming 12 10-30-2011 08:13 PM
Comparison of 2 files and generating the output based on comparison Deepu Perl Misc 1 02-07-2011 03:09 PM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
Error in Ruby text comparison? Greg Willits Ruby 6 10-27-2007 10:15 PM
Number comparison error PW ASP General 7 05-25-2004 06:05 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57