Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Ruby

(alternative version)

Date:05/18/05
Author:Greg T.
URL:n/a
Comments:10
Info:n/a
Score: (3.05 in 167 votes)
# There's more than one 'nice' way to do it ;-)
# www.ruby-lang.org

puts; puts "   It's beer song time!"; puts
 
def bottles(n)
  n == 1 ? "#{n} bottle" : "#{n} bottles"
end
 
@count = 99

@count.downto(1)  {
puts <<BEERSONG
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   #{bottles(@count)} of beer on the wall
   #{bottles(@count)} of beer
   Take one down, pass it around
   #{bottles(@count -= 1)} of beer on the wall
BEERSONG
}
 
puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
puts; puts "   No more beer on the wall :-("

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Using continuations, singleton classesVictor Borja09/15/069
minimal versionAnonymous05/18/053
object-oriented versionMike Gertz04/20/052
In wordsDaniel Straight07/10/061
shows inheritance, iterators, yield, etcKian Wright06/10/050
wall-based OO versionKevin Baird07/07/052
monkeypatch and anonymous procsJ. B. Rainsberger04/04/070
Readably re-opening Integer, teetotallerEric Budd01/06/080

Comments

>>  Ivan said on 08/08/05 22:18:44

Ivan I love Ruby...

Looks great, good work.

>>  AEDE said on 10/28/05 15:14:14

AEDE fine work...

I just personnaly prefer the %s syntax for s management ;)

def bottles(n); "#{n} bottle%s" % n > 1 ? 's' : ''; end

>>  binary42 said on 03/10/06 20:26:38

binary42 This example works fine but the use of @counter is somewhat ugly and unneeded. An alternate version might look like this:

puts "\n It's beer song time!\n"

bottles = lambda {|n| n == 1 ? "#{n} bottle" : "#{n} bottles"}
sep = "~" * 32

99.downto 1 do |n|
puts "#{sep}
#{bottles[n]} of beer on the wall
#{bottles[n]} of beer
Take one down, pass it around
#{bottles[n - 1]} of beer on the wall"
end

puts sep
puts "\n No more beer on the wall :-("

That might be a bit better... but as you said, there is more than one way to do it.

>>  April Bradshaw said on 09/29/06 19:31:00

April Bradshaw Thank you so much for this help. I've been looking for the code for days now and couldn't find anything to work.

>>  Ivan Aleman said on 10/17/06 22:50:25

Ivan Aleman Hello, here's my version, keep in mind that I been 24 hrs learning Ruby :P

puts 'time to sing 99 bottles'.capitalize
bottles = 99
while bottles != 0
puts bottles.to_s + ' bottles of beer on the wall, ' + bottles.to_s + ' bottless of beer.'
bottles = bottles - 1
puts 'Take one and pass it around, ' + bottles.to_s + ' of beer on the wall.'
puts ''
end
if bottles == 0
puts 'No more beer, and we\'re not drunk yet.'
end

>>  Scott Sweeney said on 12/05/06 21:11:15

Scott Sweeney I used the same code as Ivan Aleman above me, only I fixed the code so that at the end of the song it correctly says "2 bottles," "1 bottle," and "0 bottles," respectively.

Scott
--------------------------------------------------------------------------

bottles = 99

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

if bottles == 2
puts bottles.to_s + ' bottles of beer on the wall, ' + bottles.to_s + ' bottles of beer.'
bottles = bottles - 1
puts 'Take one down and pass it around, ' + bottles.to_s + ' bottle of beer on the wall.'
puts ''
end

if bottles == 1
puts bottles.to_s + ' bottle of beer on the wall, ' + bottles.to_s + ' bottle of beer.'
bottles = bottles - 1
puts 'Take the last one down and pass it around, ' + bottles.to_s + ' bottles of beer on the wall.'
puts ''
end

if bottles == 0
puts 'No more beer, and we\'re not drunk yet.'
end

>>  Comrade Smack said on 05/24/07 21:36:38

Comrade Smack I think there's a more fun solution.

require 'net/http'
h = Net::HTTP.new('www.99-bottles-of-beer.net', 80)
resp, data = h.get('/lyrics.html', nil)
byte = data.each_byte {|y|}

if resp.message == "OK"
for x in 3516..16297
putc byte[x]
end
end


>>  jeffman said on 07/12/07 20:39:46

jeffman # 99 bottles of beer on the wall
bottles = 100

100.times do
bottles -= 1
if bottles != 0
puts bottles.to_s + " bottles of beer on the wall"
puts bottles.to_s + " bottles of beer"
puts "take one down and pass it around "
puts (bottles - 1).to_s + " bottles of beer on the wall"
puts "__"
end # end if
end

>>  Kris Hedges said on 01/08/08 05:54:10

Kris Hedges Here it is via iterator.

101.times do |beers|
if beers >= 100
99.times do
beers -=1
puts beers.to_s + " bottles of beer on the wall!"
puts beers.to_s + " bottles of beer!"
puts "Take one down pass it around"
puts (beers-1).to_s + " bottles of beer on the wall!"
end
elsif
beers += 100
end
end

>>  Kedar Mhaswade said on 01/01/09 10:38:44

Kedar Mhaswade This program is wrong since it does not write the authentic beer song. The last lines are printed as:
1 bottle of beer
Take one down, pass it around
0 bottles of beer on the wall

and they should be:

1 bottle of beer on the wall,1 bottle of beer.
Take one down and pass it around,no more bottles of beer on the wall.


Here is a better version, gotten to by reading only up to Chapter 6 in Chris Pine's excellent tutorial at http://pine.fm/LearnToProgram/?Chapter=06

#!/usr/bin/ruby
bottles=99
suffixes=['no more bottles', '1 bottle']
i=2
while i <= bottles
suffixes=suffixes+[i.to_s + ' bottles']
i=i+1
end
while bottles > 0
puts suffixes[bottles] + " of beer on the wall," + suffixes[bottles] + " of beer."
puts "Take one down and pass it around," + suffixes[bottles-1] + " of beer on the wall."
bottles=bottles-1
end
puts "No more bottles of beer on the wall, no more bottles of beer."
puts "Go to the store and buy some more, 99 bottles of beer on the wall."

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: