The Numeric class

#1

I'm doing the If, Else, and Elsif exercise and wanted to make it so that the code wouldn't progress to evaluating a and b unless a and b are definitely numbers. So I'm trying to use the until loops to check that a and b are numbers, but I'm not sure I'm using is_a? correctly. I want the loop to assess whether or not the variables belong to the Numeric class, but even when a number is entered, the condition won't evaulate to true, so the code just gets stuck in the until loop. How can I get the loop to break when a number is entered? Thanks.

``````print 'Enter number a:'
a = gets.chomp
until a.is_a?(Numeric) == true
print "That's not a number, enter number a:"
a = gets.chomp
end

print 'Enter number b:'
b = gets.chomp
until b.is_a?(Numeric) == true
print "That's not a number, enter number b:"
b = gets.chomp
end

if a == b
print 'a equals b.'
elsif a < b
print 'a is less than b.'
else
print 'a is more than b.'
end``````

#2

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