[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

more newbie help with classes and methods please

simonh

7/18/2006 4:32:00 PM

I posted a question a week or so now which I got some great help with.
Here I am again I'm afraid! What I am now struggling with is how to add
help. What I want is if the user gets the input wrong three times, I'd
like to print to print

> Press 'h' for help
> h
> You must enter your age as two digits. i.e if you are twenty five, type 25.


here is the original method:
-----------------------------------------------
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 '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
---------------------------------------------------

Should I create a class and add a 'show_help' method (putting the
'check_age' method in there as well)? If so, how/where do I call that
method? The method in the above code calls itself. I am a complete
programming newbie by the way.

Maybe we could expand this program to help other new programmers like
myself? The jukebox example in Pickaxe2 seems to fizzle out.

23 Answers

Daniel Hollocher

7/19/2006 1:52:00 AM

0

My suggestion would be to add that behavior right when you are getting
the user input. Add a check at the beggining, and keep it out if the
if statement.

Also, I would change the if/elsif statements to a case statement. The
case statement is perfect for what you are trying to do.

Dan

On 7/18/06, simonh <simonharrison@fastmail.co.uk> wrote:
> I posted a question a week or so now which I got some great help with.
> Here I am again I'm afraid! What I am now struggling with is how to add
> help. What I want is if the user gets the input wrong three times, I'd
> like to print to print
>
> > Press 'h' for help
> > h
> > You must enter your age as two digits. i.e if you are twenty five, type 25.
>
>
> here is the original method:
> -----------------------------------------------
> 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 '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
> ---------------------------------------------------
>
> Should I create a class and add a 'show_help' method (putting the
> 'check_age' method in there as well)? If so, how/where do I call that
> method? The method in the above code calls itself. I am a complete
> programming newbie by the way.
>
> Maybe we could expand this program to help other new programmers like
> myself? The jukebox example in Pickaxe2 seems to fizzle out.
>
>
>

S Wayne

7/19/2006 2:10:00 AM

0

A couple of things:

First, as Daniel already said, why not put the information right in the
loop? However, don't go recursive there, I'd do a loop instead:

age = 0
target_age = 30..65 # ;)
while ! target === age
print "Please enter your age: (as two digits): "
result = gets
age = result.chomp.to_i
case age
when age < 30
puts 'Sorry, too young.'
when age > 75
puts 'Sorry, too old.'
else
puts 'Hava a nice holiday!'
return age
end
puts "Remember, age must be entered as two digits, e.g. 35"
end

check_age


simonh

7/19/2006 7:53:00 AM

0

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

simonh

7/19/2006 9:02:00 AM

0

decided to have a go at using the case statement. Why won't this work?

-------------------------------------------------------------------------------------
def double_it
print "Please enter a whole number (or type 'h' for help) : "
num = gets.chomp
case num
when num == 'h'
puts
puts "A whole number is 45 for example, or 12564."
puts
when num.to_i > 1
num * num
print num.to_s
when num.to_i <= 0
puts "The number must be positive!"
end
end
double_it
gets
--------------------------------------------------------------------------------------

dblack

7/19/2006 9:34:00 AM

0

dblack

7/19/2006 9:39:00 AM

0

simonh

7/19/2006 9:50:00 AM

0

Thanks David. Great book by the way. Easier for a beginner than
pickaxe. Ever considered writing a 'Ruby Projects' book. Start with
some simple program ideas and build them up to something useful?
Demonstrating all the syntax/conventions of Ruby. I'm happy to put down
a deposit!

Anyway,

> > case age
> > when age < 30
>
> Hang on.... Remember that a case statement works like this:
>
> case a
> when b
>
> is equivalent to:
>
> if b === a
>
> So what you've got is:
>
> if (age < 30) === age
>
> which is going to be either:
>
> if true === age
>
> or
>
> if false === age
>
> Neither of those is ever true when age is an integer, so the test will
> never succeed.


I don't quite understand I'm afraid. I have a variable I want to check
a number of conditions against. Can't i do this with the case
expression? Further explanation would be greatly appreciated.



>
> A simple if construct will work fine:
>
> if age < 30
> ...
> elsif age > 75
> ...
> else
> ...
> end


Thats what I thought! What is the main advantage of case over if? Or
when would you be more likely to use one than the other?


>
> > puts 'Sorry, too young.'
> > when age > 75
> > puts 'Sorry, too old.'
> > else
> > puts 'Hava a nice holiday!'
> > return age
> > end
> > puts "Remember, age must be entered as two digits, e.g. 35"
> > end
> >
> > check_age
>
> No such method :-)


I tried wrapping that code in a method and got an error. The main thing
I am trying to understand is where to put the code to print out help.

simonh

7/19/2006 9:59:00 AM

0

I end with gets because I'm on Windows which usually shuts the window
immediately unless gets is there.

simonh

7/19/2006 10:07:00 AM

0

would this be an example of using case:

case favourite_colour
when white
print 'white'
when blue
print 'blue'
when orange
print 'orange'
end

instead of

var = favourite_colour
if white
print 'white'
elsif blue
print 'blue'
....
end

dblack

7/19/2006 10:21:00 AM

0