# 99 bottles of beer...pg57

**URL:** <https://www.ruby-forum.com/t/99-bottles-of-beer-pg57/75133>\
**Category:** Ruby\
**Created:** [November 7, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/99-bottles-of-beer-pg57/75133 "2006-11-07T18:39:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamison\_edmonds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@jamison\_edmonds](https://www.ruby-forum.com/u/jamison_edmonds)\
**Post date:** [November 7, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/99-bottles-of-beer-pg57/75133/1 "2006-11-07T18:39:10Z")

</div>

Hi

Iâ€™m reading the book Learn to Program, by Chris P… I  
wanted to see if my program 99 bottles of beer on the wall, from (pg.57  
or [http://pine.fm/LearnToProgram/?Chapter=06](http://pine.fm/LearnToProgram/?Chapter=06) ) is  
o.k.?

An online search thru Google found Ruby-Talk, and I  
instantly bookmarked it.

Now Iâ€™m on the list.

Any thoughts would be appreciated 😉

Hears my try at it

Thanks

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

---

<div class="post-metadata">

**Author:** ![jamison\_edmonds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@jamison\_edmonds](https://www.ruby-forum.com/u/jamison_edmonds)\
**Post date:** [November 7, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/99-bottles-of-beer-pg57/75133/2 "2006-11-07T18:51:27Z")

</div>

Hi Jamison, I came up with a shorter version. I think it is more  
efficient, but I am about as new as you are. Here was my code:

bottles = 99  
while bottles \> 0  
puts bottles.to\_s + " bottles of beer on the wall, " + bottles.to\_s +  
" bottles of beer. Take one down and pass it around, " + (bottles  
= bottles - 1).to\_s + " bottles of beer."  
end

---

<div class="post-metadata">

**Author:** ![jamison\_edmonds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@jamison\_edmonds](https://www.ruby-forum.com/u/jamison_edmonds)\
**Post date:** [November 7, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/99-bottles-of-beer-pg57/75133/3 "2006-11-07T18:59:28Z")

</div>

Hi Jamison,

The first thing I would do is remove the duplication with:

puts ‘99, Bottles of beer on the wall, 99 bottles of beer,’  
puts ‘take one down, pass it around,’

and put that in the loop. Since your code in the loop does the same  
thing, you could just decrement the value of first after printing the  
current lyrics, and let that only reside in 1 place instead of two.  
This way, if the song changes to something like “take one down, take a  
big swig and pass it around,” you only need to change it in one place.

Also, it doesn’t appear the “if first != 1” block will _never_ be  
executed (and I don’t see a need for it even if it was), so you could  
remove it and just decrement the value of first.

Finally, I think the variable name of first could be better named as to  
what it represents. After the first bottle of beer is removed from the  
wall, it no longer represents the first bottle. So, I might go with  
something like current\_bottle, or just anything that describes it better  
really.

-Sam

---

<div class="post-metadata">

**Author:** ![jamison\_edmonds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@jamison\_edmonds](https://www.ruby-forum.com/u/jamison_edmonds)\
**Post date:** [November 7, 2006, 7:17pm UTC](https://www.ruby-forum.com/t/99-bottles-of-beer-pg57/75133/4 "2006-11-07T19:17:33Z")

</div>

On 11/7/06, jamison edmonds [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any thoughts would be appreciated 😉

Instead of using a ‘while’ loop (unless that’s what the book is  
teaching you) I think the following is more Ruby-esque:

99.downto( 1 ) do |num|  
puts “#{num} bottles of beer on the wall, #{num} bottles of beer,”  
puts “take one down, pass it around,”  
end  
puts ‘0,Bottles of beer on the wall ;)’

Note the use of string interpolation also instead of .to\_s and +

---

<div class="post-metadata">

**Author:** ![jamison\_edmonds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@jamison\_edmonds](https://www.ruby-forum.com/u/jamison_edmonds)\
**Post date:** [November 7, 2006, 7:43pm UTC](https://www.ruby-forum.com/t/99-bottles-of-beer-pg57/75133/5 "2006-11-07T19:43:38Z")

</div>

you could drop the unless and the final line. ( num = 1, num-1 = 0 😉

99.downto( 1 ) do |num|  
puts  
puts “#{num} bottles of beer on the wall, #{num} bottles of beer,”  
puts “take one down, pass it around,”  
puts “#{num-1} bottles of beer on the wall”  
end

Altho I think the book hasn’t covered the enumeration type stuff yet  
like downto.

---

<div class="post-metadata">

**Author:** ![jamison\_edmonds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@jamison\_edmonds](https://www.ruby-forum.com/u/jamison_edmonds)\
**Post date:** [November 7, 2006, 7:17pm UTC](https://www.ruby-forum.com/t/99-bottles-of-beer-pg57/75133/6 "2006-11-07T19:17:34Z")

</div>

Phrogz wrote:

> 99.downto( 1 ) do |num|  
> puts “#{num} bottles of beer on the wall, #{num} bottles of beer,”  
> puts “take one down, pass it around,”  
> end  
> puts ‘0,Bottles of beer on the wall ;)’

Oops, I am, of course, missing one line of the song, and a nice newline  
for breaks. Should be more like:

99.downto( 1 ) do |num|  
puts  
puts “#{num} bottles of beer on the wall, #{num} bottles of beer,”  
puts “take one down, pass it around,”  
puts “#{num-1} bottles of beer on the wall” unless num == 1  
end  
puts ‘0 bottles of beer on the wall ;)’
