Beginner's question

P

Peter Johnsson

This question may be a bit too "simple" for this forum which seems to
target intermediate and advanced users, but if that's the case someone
could perhaps direct me to a place which is better for asking this
question?

I have just recently decided to try and learn programming (namely
Ruby), and in order to do so I've been using a tutorial called "learn to
program" (http://pine.fm/LearnToProgram/).
Everything has gone just fine until I reached the 6:th chapter (Flow
Control) in which the author teaches about among other things branching
and loop-methods.
I think I understand most of it, however, I'm having very large
difficulties with the " A Few Things to Try" part of the chapter.
I find the "99 beer bottles"-program utterly impossible to do. I
realise that I should somehow use the methods listed on the page, but I
just can seem to know how. My initial idea is to create a variable
called "bottles" which at first is 99 and then subtract 1 in every new
verse until the variable hits 0, where I was thinking I could use the
"while"-method to end the program. Each new value would be inserted into
the lyrics. It would look something like this I suppose (please don't
laugh, I know it doesn't really do anything at all).

bottles = 99

while bottles != 0
# Need help here.
end

I stumbled upon a site which showed how one scould program this in Ruby
(http://99-bottles-of-beer.net/language-ruby-1272.html), but the problem
is it doesn't use the same methods. It's more advanced since it creates
classes and defines methods, something which I think lies a bit too far
ahead for me, and I somehow want to learn things in "the right order".

I'm thankful for any tips as well as code-examples. Thank you very much.
 
J

James Gray

This question may be a bit too "simple" for this forum which seems to
target intermediate and advanced users, but if that's the case someone
could perhaps direct me to a place which is better for asking this
question?

We welcome all questions.
bottles = 99

while bottles != 0
# Need help here.
end

You are doing just fine here. Great start.

The most important thing is to try to move in very small steps. Try
to add just one more little thing to your code, then another thing,
and another. Each time moving closer to what you really want. That's
how programmers manage the complexity of tackling big problems we
can't keep all in our head at one time.

So, for a next step, can you just getting it printing the song lyrics
inside the loop? They don't need to reflect the decreasing bottle
count yet, just get it printing something 99 times. Of course, to do
that, you will need to reduce the bottle variable each time so it
eventually hits zero and cancels the loop. Given that, you need to
add one or more puts() statements to handle the printing and a line
that does some math on the bottles variable.

Is that enough of a hint? If not, come back and I'll show you some
code.

Good luck and welcome to Ruby!

James Edward Gray II
 
J

Jeff Patterson

Peter said:
This question may be a bit too "simple" for this forum which seems to
target intermediate and advanced users, but if that's the case someone
could perhaps direct me to a place which is better for asking this
question?

I have just recently decided to try and learn programming (namely
Ruby), and in order to do so I've been using a tutorial called "learn to
program" (http://pine.fm/LearnToProgram/).
Everything has gone just fine until I reached the 6:th chapter (Flow
Control) in which the author teaches about among other things branching
and loop-methods.
I think I understand most of it, however, I'm having very large
difficulties with the " A Few Things to Try" part of the chapter.
I find the "99 beer bottles"-program utterly impossible to do. I
realise that I should somehow use the methods listed on the page, but I
just can seem to know how. My initial idea is to create a variable
called "bottles" which at first is 99 and then subtract 1 in every new
verse until the variable hits 0, where I was thinking I could use the
"while"-method to end the program. Each new value would be inserted into
the lyrics. It would look something like this I suppose (please don't
laugh, I know it doesn't really do anything at all).

bottles = 99

while bottles != 0
# Need help here.
end

I stumbled upon a site which showed how one scould program this in Ruby
(http://99-bottles-of-beer.net/language-ruby-1272.html), but the problem
is it doesn't use the same methods. It's more advanced since it creates
classes and defines methods, something which I think lies a bit too far
ahead for me, and I somehow want to learn things in "the right order".

I'm thankful for any tips as well as code-examples. Thank you very much.

When doing a loop of this type, concentrate on what is static and what
needs to change at each iteration. In each verse the only thing that
changes are the numbers so:

while bottles > 0
after_this_loop = bottles-1
puts "#{bottles} of beer on the wall. #{bottles} of beer. Take one
down and pass it around #{bottles=bottles-1} bottles of beer on the
wall"
bottles = bottles -1
end

A ruby programmer would probably do something like:

99.downto(1) do |bottles|
puts "#{bottles} of beer on the wall. #{bottles} of beer. Take one down
and pass it around #{bottles=bottles-1} bottles of beer on the wall"
end

Now, figure out how to make the verse come out right when bottles = 1
 
J

Jeff Patterson

whoops, cut and paste error

the first loop should be
while bottles > 0
after_this_loop = bottles-1
puts "#{bottles} of beer on the wall. #{bottles} of beer. Take one
down and pass it around #{bottles-1} bottles of beer on the wall"
bottles = bottles -1
 
J

James Gray

puts "#{bottles} of beer on the wall. #{bottles} of beer. Take one
down and pass it around #{bottles=3Dbottles-1} bottles of beer on the
wall"

Learn to Program doesn't show this kind of interpolation, if my memory =20=

serves, so Peter is probably more use to seeing this as:

puts bottles.to_s + ' of beer on the wall=85'

James Edward Gray II=
 
L

Lloyd Linklater

Peter said:
bottles = 99

while bottles != 0
# Need help here.
end

My $0.02:

bottles = 99
bob = " bottles of beer "
otw = "on the wall."

while bottles != 0 do
puts bottles.to_s + bob + otw
puts bottles.to_s + bob
puts "Take one down and pass it around"
bottles -= 1
puts bottles.to_s + bob + otw
puts ""
end

Here is a way to do it with an implicit decrementor:

bob = " bottles of beer "
otw = "on the wall."

99.downto(1) do |bottles|
puts bottles.to_s + bob + otw
puts bottles.to_s + bob
puts "Take one down and pass it around"
puts (bottles - 1).to_s + bob + otw
puts ""
end
 
S

Stefano Crocco

This question may be a bit too "simple" for this forum which seems to
target intermediate and advanced users, but if that's the case someone
could perhaps direct me to a place which is better for asking this
question?

I have just recently decided to try and learn programming (namely
Ruby), and in order to do so I've been using a tutorial called "learn to
program" (http://pine.fm/LearnToProgram/).
Everything has gone just fine until I reached the 6:th chapter (Flow
Control) in which the author teaches about among other things branching
and loop-methods.
I think I understand most of it, however, I'm having very large
difficulties with the " A Few Things to Try" part of the chapter.
I find the "99 beer bottles"-program utterly impossible to do. I
realise that I should somehow use the methods listed on the page, but I
just can seem to know how. My initial idea is to create a variable
called "bottles" which at first is 99 and then subtract 1 in every new
verse until the variable hits 0, where I was thinking I could use the
"while"-method to end the program. Each new value would be inserted into
the lyrics. It would look something like this I suppose (please don't
laugh, I know it doesn't really do anything at all).

bottles = 99

while bottles != 0
# Need help here.
end

I stumbled upon a site which showed how one scould program this in Ruby
(http://99-bottles-of-beer.net/language-ruby-1272.html), but the problem
is it doesn't use the same methods. It's more advanced since it creates
classes and defines methods, something which I think lies a bit too far
ahead for me, and I somehow want to learn things in "the right order".

I'm thankful for any tips as well as code-examples. Thank you very much.

Inside the while loop, you need to do essentially three things:
a) display the verse related to the current number of bottles
b) break a bottle (that is, decrease the variable bottle by one)
c) display the verse related to the new number of bottles

Each of these goals may be translated in a line of code:

while bottles !=0
puts bottles.to_s + "bottles of beer on the wall, " + bottles.to_s + "bottles of beer."
bottles -= 1
puts "Take one down and pass it around, "+ bottles.to_s + "bottles of beer on the wall."
end

This works almost perfectly, except for the fact that it doesn't treat the
case of one bottle and of 0 bottle in a special way (when there's only one
bottle, it should print 1 bottle, when there are no more bottles, it should
write so). To correct this, you should insert some if statements inside the
while loop.

I hope this helps

Stefano
 
L

Lloyd Linklater

I just have to add one that has another hopefully helpful bit in it.

def printFromMethod(i, s)
puts "#{i} bottles of beer" + s
end

99.downto(1) do |bottles|
printFromMethod(bottles, " on the wall.")
printFromMethod(bottles, ".")
puts "Take one down and pass it around."
printFromMethod(bottles - 1, " on the wall.")
puts ""
end
 
P

Peter Johnsson

Thank you for all of your responses. I'm sorry for being so slow with
responding, but you guys have given me a lot of helpful information so
I've been trying to focus on looking at your examples as well as
understanding them. I'll keep reading and try to make sure I get it
right at last. Thanks a lot, you've helped me very much!
 
T

Todd Benson

Thank you for all of your responses. I'm sorry for being so slow with
responding, but you guys have given me a lot of helpful information so
I've been trying to focus on looking at your examples as well as
understanding them. I'll keep reading and try to make sure I get it
right at last. Thanks a lot, you've helped me very much!

Here's a weird one that won't help you that much. It's just for fun.
You can separate 1, 0, and other integers with 1 / i. For example...

[0, 1, 2, 3, 4, 5].reverse.map! {|i| ((1 / i).zero? ? "a" : "b" rescue "c")}

=> ["a", "a", "a", "a", "b", "c"]

Why would you do it this way? Well, you probably wouldn't. You'd be
just fine with if/then/elsif/else and case constructs, not to mention
this strange use of "rescue" for a conditional predicate.

To the OP, you definitely are on the right track! Just remember that
Ruby has some behind-the-scenes methods that do this type of work for
you.

Todd
 
P

Peter Johnsson

Haha! finally. After a plentyful of hours I have at last finished a
working code. Completely inept and lacking of grace, I know, but still!
At last it is finished.
Just thought I would let you guys know since you helped me so much by
providing me with code-examples and tips. I know I've thanked you once
already, but anyways once again, thanks!
I also read some of the advanced coding, and even though I can't
understand half of it I found it pleasureable to read. It also spurs me
to work harder so that one day I can (hopefully) understand it.

Here is the code by the way:

--------------
bottles = 99
line1 = " bottles of beer on the wall, "
line2 = " bottles of beer."
line3 = "Take one down and pass it around, "
line4 = "bottles of beer on the wall."

while bottles != 0
if bottles != 1
firstpar = bottles.to_s + line1 + bottles.to_s + line2
secpar = line3 + bottles.to_s + line4
puts firstpar
bottles -= 1
puts secpar
else
line1["bottles"] = "bottle"
line2["bottles"] = "bottle"
line4["bottles"] = "no more bottles"
firstpar = bottles.to_s + line1 + bottles.to_s + line2
secpar = line3 + line4
puts firstpar
bottles -= 1
puts secpar
end
end

line1 = " bottles of beer on the wall, "
line2 = " bottles of beer."
firstpar = "No more " + line1 + "no more " + line2
secpar = "Go to the store and buy some more, 99 bottles of beer on the
wall."
puts firstpar
puts secpar
 
M

Marc Heiler

I also read some of the advanced coding, and even though I can't
understand half of it I found it pleasureable to read.

Dont worry too much about understanding everything. If it is too
complicated, then ask whether you even need it right now.
In its core, ruby is short and very beautiful. My favourite rule of
thumb is that the more you write (in ruby) the shorter and more elegant
a solution gets (of course this is not always true, and complex problems
will still be harder to solve elegantly and in a concise manner than
simple problems, but you get the point)

If one wants to be confused, one can listen to an explanation of a
Haskell coder - what are Monads and more important why this is important
to know. :>
 
J

Jeff Patterson

Good job. One thought though, as you are learning to program always keep
in mind what is actually happening as your code is executed with an eye
towards minimizing decision points, each of which which take time. For
example, in your loop each time through two checks are made in a row:
while bottles != 0
if bottles != 1

If instead you did:
while bottles != 1
non-special code goes here..
bottles=bottles -1
end

# note bottles = 1 here
#print out special case verse


See how we've eliminated the unnecessary decision point? Such
optimizations are especially important inside loops where the interior
code gets executed over and over.

Cheers
Jeff
 
T

Tim Hunter

Peter said:
Haha! finally. After a plentyful of hours I have at last finished a
working code. Completely inept and lacking of grace, I know, but still!
At last it is finished.

Congratulations! I know how good this feeling is.

I wrote my first program in 1979. It was a dozen lines of FORTRAN that
computed the roots of a quadratic equation. I typed the code into a
machine that produced punched cards, fed the cards into the hopper of a
punch card reader, and then waited beside a line printer the size of a
commericial washing machine. Some time between a few minutes and a few
hours later the output appeared, printed on a giant sheet of continuous
fold paper patterned with alternating light and dark green lines. I took
the paper home with me and spent more time than I'd care to admit just
looking at it.

That was 29 years ago and I've never looked back. I hope the hours you
spend programming are as rewarding as mine have been.
 
T

thufir

So, for a next step, can you just getting it printing the song lyrics
inside the loop? They don't need to reflect the decreasing bottle count
yet, just get it printing something 99 times. Of course, to do that,
you will need to reduce the bottle variable each time so it eventually
hits zero and cancels the loop. Given that, you need to add one or more
puts() statements to handle the printing and a line that does some math
on the bottles variable.


In a sense, an infinite loop here, where it just prints:


99 bottles of beer
99 bottles of beer
99 bottles of beer
...


is ok because it gives you something to work *from*. You know that the
loop is executing, you know that the print statements are working...

then the question becomes: why doesn't the number decrease?

While I haven't read the tutorial, I hope that this helps :)



-Thufir
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top