This is not working

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?

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs