Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Beginner's question

Reply
Thread Tools

Beginner's question

 
 
Peter Johnsson
Guest
Posts: n/a
 
      03-24-2008
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
James Gray
Guest
Posts: n/a
 
      03-24-2008
On Mar 24, 2008, at 9:05 AM, Peter Johnsson wrote:

> 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


 
Reply With Quote
 
 
 
 
Jeff Patterson
Guest
Posts: n/a
 
      03-24-2008
Peter Johnsson wrote:
> 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jeff Patterson
Guest
Posts: n/a
 
      03-24-2008


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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
James Gray
Guest
Posts: n/a
 
      03-24-2008
On Mar 24, 2008, at 9:34 AM, Jeff Patterson wrote:

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

 
Reply With Quote
 
Lloyd Linklater
Guest
Posts: n/a
 
      03-24-2008
Peter Johnsson wrote:
> 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Stefano Crocco
Guest
Posts: n/a
 
      03-24-2008
On Monday 24 March 2008, Peter Johnsson wrote:
> 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


 
Reply With Quote
 
Lloyd Linklater
Guest
Posts: n/a
 
      03-24-2008
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Peter Johnsson
Guest
Posts: n/a
 
      03-24-2008
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!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      03-24-2008
On Mon, Mar 24, 2008 at 10:02 AM, Peter Johnsson <> wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57