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.14 in 150 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/064
In wordsDaniel Straight07/10/061
object-oriented versionMike Gertz04/20/052
shows inheritance, iterators, yield, etcKian Wright06/10/050
Readably re-opening Integer, teetotallerEric Budd01/06/080
wall-based OO versionKevin Baird07/07/052
minimal versionAnonymous05/18/053
monkeypatch and anonymous procsJ. B. Rainsberger04/04/070

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.

>>  Anonymous said on 08/05/06 03:21:12

Anonymous I am very new to Ruby... but wouldn't $counter be better than @counter? Shouldn't @counter be reserved for use in classes? Sorry if I'm totally wrong...

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

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: