[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Real newbie question about methods

simonh

7/10/2006 5:40:00 PM

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

15 Answers

Patrick Hurley

7/10/2006 5:51:00 PM

0

On 7/10/06, simonh <simonharrison@fastmail.co.uk> wrote:
> 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
>

Probably something like:

def check_age(age)
eligible = 18..30
if age < 18
print "Sorry, too young."
elsif eligible === age
print "Have a great holiday!"
true # provide a positive result
else
print "Input not understood."
end
end

print "Please enter your age: "
check_age(gets.chomp.to_i)

# of course I would use a case
# make eligble a constant
# and if required provide the flow control (probably not recursive) in
another function

Good luck
pth

pere.noel

7/10/2006 5:54:00 PM

0

simonh <simonharrison@fastmail.co.uk> wrote:

> 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
--
une bévue

Elliot Temple

7/10/2006 5:56:00 PM

0


On Jul 10, 2006, at 10:40 AM, simonh wrote:

> 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

you don't want recursion.

def check_age(age)
eligible = 18..30
if age < 18
print "Sorry, too young."
elsif eligible === age
print "Have a great holiday!"
else
print "Input not understood."
end
end

print "Please enter your age: "
age = gets.chomp.to_i
check_age(age)



-- Elliot Temple
http://www.cur...




simonh

7/10/2006 6:11:00 PM

0

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

Elliot Temple

7/10/2006 6:34:00 PM

0


On Jul 10, 2006, at 11:15 AM, simonh wrote:

> 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.

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

-- Elliot Temple
http://www.cur...




Elliot Temple

7/10/2006 6:36:00 PM

0


On Jul 10, 2006, at 11:33 AM, Elliot Temple wrote:

>
> On Jul 10, 2006, at 11:15 AM, simonh wrote:
>
>> 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.
>
> use regex. if they enter a number it will match /\d+/

umm better use anchors to be sure they *only* enter a number

/^\d+$/ if i recall correctly

-- Elliot Temple
http://www.cur...




studlee2@gmail.com

7/10/2006 6:38:00 PM

0

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


simonh wrote:
> 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

pere.noel

7/10/2006 6:42:00 PM

0

simonh <simonharrison@fastmail.co.uk> wrote:

> 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
--
une bévue

studlee2@gmail.com

7/10/2006 6:49:00 PM

0

One correction: make age a string (.to_s) if you want to use the regexp
>_<

I think Une's code is cleaner overall.

_Steve

studlee2@gmail.com wrote:
> 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.to_s)
>
> <Your Code Here>
>
> else
> puts "Invalid entry"
> end
>
> /-----------------------------------------------------------------/
> Hope this helps.
>
> _Steve
>
>
> simonh wrote:
> > 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

pere.noel

7/10/2006 6:56:00 PM

0

studlee2@gmail.com <studlee2@gmail.com> wrote:

> 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...
--
une bévue