S
simonh
Hi all. I'm trying to learn ruby using about 5 books(!) but keep
struggling over basic things that are obvious to you guys but don't
seem to be explained in a way my brain understands. Ruby is my first
programming language, by the way. Here is first one.
I have written this script (which works fine):-
---------------------------------------------
target = 18..30
print 'please enter your age: '
age = gets.chomp.to_i
if age < 18
puts 'sorry, too young.'
elsif target === age
puts 'have a great holiday.'
elsif age > 31
puts 'sorry, too old.'
end
gets
---------------------------------------------
What I want to do is put this in a method:-
------------------------------------------------
def check_age(age)
print "Please enter your age: "
age = gets.chomp.to_i
eligible = 18..30
if age < 18
print "Sorry, too young."
elsif eligible === age
print "Have a great holiday!"
else
print "Input not understood."
check_age(age) # trying recursion here
end
check_age()
end
------------------------------------------------
But it doesn't work. How do I get the value entered by the user into
the method parameter?
Many thanks
struggling over basic things that are obvious to you guys but don't
seem to be explained in a way my brain understands. Ruby is my first
programming language, by the way. Here is first one.
I have written this script (which works fine):-
---------------------------------------------
target = 18..30
print 'please enter your age: '
age = gets.chomp.to_i
if age < 18
puts 'sorry, too young.'
elsif target === age
puts 'have a great holiday.'
elsif age > 31
puts 'sorry, too old.'
end
gets
---------------------------------------------
What I want to do is put this in a method:-
------------------------------------------------
def check_age(age)
print "Please enter your age: "
age = gets.chomp.to_i
eligible = 18..30
if age < 18
print "Sorry, too young."
elsif eligible === age
print "Have a great holiday!"
else
print "Input not understood."
check_age(age) # trying recursion here
end
check_age()
end
------------------------------------------------
But it doesn't work. How do I get the value entered by the user into
the method parameter?
Many thanks