Forum: Ruby This is not working.

Posted by Radu M. (radu_m)
on 2012-11-28 22:26
Hi...I tried this code:

puts 'Level 1'
puts '1+1'
cal = gets.chomp
if cal == 1 + 1
puts 'Correct'
else
puts 'Incorrect:('
end

but when I type 2 it says incorrect...
Is this feature available on Ruby?
Posted by Stefano Crocco (crocco)
on 2012-11-28 22:35
(Received via mailing list)
On Thursday 29 November 2012 Radu M. wrote
>
> but when I type 2 it says incorrect...
> Is this feature available on Ruby?
>
> --
> Posted via http://www.ruby-forum.com/.

cal is a string. 1 + 1 is a number (class Fixnum to be precise). A 
string
can't be equal to a number. If you want to compare the two, you'll need 
to
convert one of the two:

if cal.to_i == 1 + 1

or

if cal == (1+1).to_s

Stefano
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.