Control Flow simple question


Why do I need to type "Integer" here? I don't see how it relates to the rest of the code, but when it isn't there the code fails. I assume it must be some kind of pre-defined object but I'm hoping someone could enlighten me as to what and why it is.

user_num = Integer(gets.chomp)

print "Integer please: "
user_num = Integer(gets.chomp)

if user_num < 0
  puts "You picked a negative integer!"
elsif user_num > 0
  puts "You picked a positive integer!"
  puts "You picked zero!"


Yes, a predefined class. By wrapping string input with the class we coerce the value to an integer, if possible. If not, Ruby throws an error.

Integer please:  a
invalid value for Integer(): "a"


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.