Thanks for replies. Just to point out, this code is not meant for
anything in particular, just an example to help me learn ruby. I wanted
to understand recursion better which I now do. Also to understand
ranges and if statements too. What I'm trying to do here is understand
how to branch inside a method.
What are the benefits of using case instead of if? I was going to look
at case a bit later on. The commented code below is what I've tried but
it does not work too well. if i type 21 its says 'have a nice holiday!'
then 'Incorrect Input' and prompts me again.
-----------------------------------------------------------------------
def check_age
print "Please enter your age (or type 'h' for help): "
age = gets.chomp
target = 18..30
valid = /^\d{2}/
# if age == 'h'
# puts
# puts "Example: if you are twenty five, type 25"
# puts
# check_age
#end
if valid.match(age)
if target === age.to_i
puts 'Have a nice holiday!'
elsif age.to_i < 18
puts 'Sorry, too young.'
elsif age.to_i > 30
puts 'Sorry, too old.'
end
else
puts "Incorrect input."
puts
check_age # Try again
end
end
check_age
gets