# Re: Learn to Program, by Chris Pine

**URL:** <https://www.ruby-forum.com/t/re-learn-to-program-by-chris-pine/93382>\
**Category:** Ruby\
**Created:** [April 1, 2006, 3:05am UTC](https://www.ruby-forum.com/t/re-learn-to-program-by-chris-pine/93382 "2006-04-01T03:05:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 1, 2006, 3:05am UTC](https://www.ruby-forum.com/t/re-learn-to-program-by-chris-pine/93382/1 "2006-04-01T03:05:04Z")

</div>

Dave wrote:

> JB wrote:

> > I too wish the author had put examples, say on some pages in the back or  
> > something.

> beer = 99  
> while beer \>= 0  
> beer = beer - 1  
> end
> 
> OK, now we’ve got the basic structure of the program - one loop, beer starts  
> at 99, and decreases until it gets to 0. But there’s no output yet.
> 
> See how you go putting some flesh on that skeleton, JB - post back when  
> you’re done.

Hi Dave,

Okay, here’s what I got, and it works too, heh. The ‘if’ had to be in  
there,  
or else it kept ending with negative numbers (usually -2). This way is  
as  
good as Bill’s, but a lot shorter (not putting you down or anything  
Bill!).

beer = 99  
while beer \>= 0  
beer = beer - 1  
if (beer - 1) \>= 0  
puts beer.to\_s + ’ ’ ‘bottles of beer on the wall,’ +  
beer.to\_s + ’ bottles of beer,’ +  
‘take one down, pass it around,’ +  
(beer - 1).to\_s + ’ ’ + ‘bottles of beer on the wall!’  
end  
end

Now, to clean it up even more…when it gets down to ‘1 bottle’, I  
don’t  
want it to say ‘1 bottles…’ (plural). How would I do this? I tried  
with an  
‘else’ in there, but for some reason I either made it an infinite loop  
or it  
kept getting the negative numbers all over again.  
I tried with an ‘else (beer - 1) == 1’ (no quotes of course and that  
didn’t  
work either.

I do appreciate all the help you guys are giving, and yes, the  
interactive  
way will make me work a little harder too to get things right, plus I’m  
the  
kind who _needs_ that kind of help or I fail miserably and give up, heh.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 1, 2006, 4:14am UTC](https://www.ruby-forum.com/t/re-learn-to-program-by-chris-pine/93382/2 "2006-04-01T04:14:49Z")

</div>

JB wrote:

> ```
> puts beer.to_s + ' ' 'bottles of beer on the wall,' +
> beer.to_s + ' bottles of beer,' +
> 'take one down, pass it around,' +
> (beer - 1).to_s + ' ' + 'bottles of beer on the wall!'
> end
> 
> ```
> 
> end

Like I told Jan, put the “beer = beer - 1” at the end of the loop - then  
you  
won’t need that “if”. By the way, that “if” covers the whole loop.  
Wouldn’t  
it be simpler just to exit the loop instead of going through again but  
doing  
nothing? Here are the two alternatives I’m suggesting:

beer = 100 # have to add 1, because the first thing we’re doing is  
subtracting 1!  
while (beer - 1) \>= 0 # a little better - no need for the inside “if”  
beer = beer - 1  
puts …  
end

beer = 99  
while beer \>= 0  
puts …  
beer = beer - 1  
end

Also, check out the typo here:

> ```
> puts beer.to_s + ' ' 'bottles of beer on the wall,' +
> 
> ```

’ ’ ‘bottles…’ is the same as ’ ’ + ‘bottles…’. You should change it  
to  
’ bottles…’.

> Now, to clean it up even more…when it gets down to ‘1 bottle’, I don’t  
> want it to say ‘1 bottles…’ (plural). How would I do this? I tried with  
> an  
> ‘else’ in there, but for some reason I either made it an infinite loop or  
> it  
> kept getting the negative numbers all over again.  
> I tried with an ‘else (beer - 1) == 1’ (no quotes of course and that  
> didn’t  
> work either.

Bill’s solution did this! You might want to read it and figure out how.  
Still, I would prefer to do it a different way:

beer = 99  
s = ‘’  
while beer \>= 0  
if beer == 1  
s = ‘’  
else  
s = ‘s’  
end  
puts … + ‘bottle’ + s + …  
beer = beer - 1  
end

> I do appreciate all the help you guys are giving, and yes, the  
> interactive  
> way will make me work a little harder too to get things right, plus I’m  
> the  
> kind who _needs_ that kind of help or I fail miserably and give up, heh.

We’ll keep you accountable. Don’t give up.

Cheers,  
Dave
