Control Flow simple question


#1



https://www.codecademy.com/courses/ruby-beginner-en-NFCZ7/0/1?curriculum_id=5059f8619189a5000201fbcb


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!"
else
  puts "You picked zero!"
end


#2

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"

#3

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