First, I want to say that I am new to ruby, and I am still learning. I am currently working on a school project where the assignment is to make a program calculate the user's input.
However, in my program I want it to only permit the user to type in numbers. If the user gives any form for letters it will get an error, and make the user try again until he gives a number. Any way of doing this?
Thank you so much, if you can help me:)
Here is my code so far:
def calculate_one #method to calculate option 1
prompt = "> " #make it look nice
puts "What is the salary for the accountant? "
print prompt
while @accountant = gets.chomp #looping
if @accountant =~ /\A\d+\z/
puts "Error: Only use numbers. Please try again"
print prompt
else
"What is the salary for the developer? "
print prompt
while @developer = gets.chomp
if @developer =~ /\A\d+\z/
puts "Error: Only use numbers. Please try again"
print prompt
else
print "The salary for an account is #{@accountant}\nThe salary for an developer is #{@developer}"
break
end
end
end
end
end