Reputation: 3
I am learning Ruby and faced with some problem.I tried to compare a sum of expressions with integer and get this return: "comparison of String with 2000 failed". Thanks a lot!
puts "Hello! Please type here your birthday date."
puts "Day"
day = gets.chomp
day.capitalize!
puts "Month"
month = gets.chomp
month.capitalize!
puts "Year"
year = gets.chomp
year.capitalize!
if month + day + year > 2000
puts "Sum of all the numbers from your birthday date is more than 2000"
else month + day + year < 2000
puts "Sum of all the numbers from your birthday date is less than 2000"
end
Upvotes: 0
Views: 72
Reputation: 230521
day = gets.chomp
Here day
is a string. And month + day + year
is a string too, only longer. To get integers, call .to_i
.
day = gets.to_i # to_i will handle the newline, no need to chomp.
# repeat for month and year
(Of course, once you converted strings to integers, you won't be able to capitalize them. It made no sense anyway.)
Upvotes: 1