Real newbie question about methods

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
 
U

Une bévue

simonh said:
But it doesn't work. How do I get the value entered by the user into
the method parameter?

def check_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 # trying recursion here
end
end

check_age


works fine
 
S

simonh

Thanks Une bévue

It seems I didn't need the parameter to the method. Also forgot to add
the second elsif (to check if too old). If I run the program and type
'a' instead of a two digit number the first if statement gets printed -
'sorry too young'

what i want is for the else statement to be run if user input is not
entered as two digits. How would I do this? Somehow got to check if
gets recieves an integer of two digits.

thanks again
 
E

Elliot Temple

Thanks Une b=E9vue

It seems I didn't need the parameter to the method. Also forgot to add
the second elsif (to check if too old). If I run the program and type
'a' instead of a two digit number the first if statement gets =20
printed -
'sorry too young'

what i want is for the else statement to be run if user input is not
entered as two digits. How would I do this? Somehow got to check if
gets recieves an integer of two digits.

use regex. if they enter a number it will match /\d+/

-- Elliot Temple
http://www.curi.us/blog/
 
S

studlee2

You can add a regular expression to see if you age is valid like this:

/-----------------------------------------------------------------/
valid = /\d{2}/ #checks for exactly 2 digits
if valid.match(age)

<Your Code Here>

else
puts "Invalid entry"
end

/-----------------------------------------------------------------/
Hope this helps.

_Steve
 
U

Une bévue

simonh said:
It seems I didn't need the parameter to the method. Also forgot to add
the second elsif (to check if too old). If I run the program and type
'a' instead of a two digit number the first if statement gets printed -
'sorry too young'

what i want is for the else statement to be run if user input is not
entered as two digits. How would I do this? Somehow got to check if
gets recieves an integer of two digits.

def check_age
print "Please enter your age: "
age = gets.chomp.to_i
eligible = 18..30

if age == 0
print "Please enter a two digits number in « 18..30 »."
check_age
elsif eligible === age
print "Have a great holiday!"
else age < 18
print "Sorry, too young."
end
end

check_age


the "gets.chomp.to_i" returns 0 when only alphabetic chars. ie :

"toto" => 0

or :

"blahblah27yyyiyiyiyui" => 0
 
U

Une bévue

valid = /\d{2}/ #checks for exactly 2 digits

this woundn't help because of gets.chomp returns always strings and
gets.chomp.to_i returns Fixnum with 0 in case of any alpha betic char in
gets...
 
S

studlee2

Agreed that the regexp wouldn't work for your code. I would have
written it like this (like I said before your code is cleaner)

def check_age
print "Please enter your age: "
age = gets.chomp
target = 18..30
valid = /\d{2}/

if valid.match(age)
if target === age.to_i
puts 'Have A Nice Holiday'
elsif age.to_i < 18
puts 'Too Young'
elsif age.to_i > 30
puts 'Too Old'
end
else
puts "Usage: <18..30>"
check_age # Try again
end
end

check_age

_Steve
 
S

simonh

Agreed that the regexp wouldn't work for your code. I would have
written it like this (like I said before your code is cleaner)

def check_age
print "Please enter your age: "
age = gets.chomp
target = 18..30
valid = /\d{2}/

if valid.match(age)
if target === age.to_i
puts 'Have A Nice Holiday'
elsif age.to_i < 18
puts 'Too Young'
elsif age.to_i > 30
puts 'Too Old'
end
else
puts "Usage: <18..30>"
check_age # Try again
end
end

check_age

_Steve

thanks Steve, I like that. a nested if that checks against a rexexp
first. mind if I add it to my Library Of Useful Code Samples For When I
Get Stuck?
 
S

studlee2

Go for it. We're here to help >_<


thanks Steve, I like that. a nested if that checks against a rexexp
first. mind if I add it to my Library Of Useful Code Samples For When I
Get Stuck?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,242
Latest member
KendrickKo

Latest Threads

Top