Reputation: 289
The below ruby code is not working as expected. It doesn't seem to recognise the 1..3
range and is only reading the else
condition. If I gave a number instead of a range, it works though. Not sure where I'm going wrong.
print "Enter your cost: "
cost = gets.chomp
case cost
when 1..3
puts "inexpensive"
when 3..5
puts "affordable"
else puts "no comments"
end
Upvotes: 0
Views: 68
Reputation: 168081
The input you get from gets
is always a string, so it will never match a number range. To convert it to an integer, do this:
cost = gets.to_i
You can directly put it in case statement like so
case gets.to_i
Upvotes: 0
Reputation: 230306
You're trying to match a string against an integer range. That's not going to work. Make an integer.
cost = gets.chomp.to_i
Upvotes: 1