Language Ruby
(alternative version)
| Date: | 05/18/05 |
| Author: | Greg T. |
| URL: | n/a |
| Comments: | 10 |
| Info: | n/a |
| Score: |
# 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
| Version | Author | Date | Comments | Rate |
|---|---|---|---|---|
| Using continuations, singleton classes | Victor Borja | 09/15/06 | 4 | |
| In words | Daniel Straight | 07/10/06 | 1 | |
| object-oriented version | Mike Gertz | 04/20/05 | 3 | |
| shows inheritance, iterators, yield, etc | Kian Wright | 06/10/05 | 0 | |
| Readably re-opening Integer, teetotaller | Eric Budd | 01/06/08 | 0 | |
| wall-based OO version | Kevin Baird | 07/07/05 | 2 | |
| minimal version | Anonymous | 05/18/05 | 3 | |
| monkeypatch and anonymous procs | J. B. Rainsberger | 04/04/07 | 0 |
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!
Comments
Looks great, good work.
I just personnaly prefer the %s syntax for s management
def bottles(n); "#{n} bottle%s" % n > 1 ? 's' : ''; end
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.
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
--------------------------------------------------------------------------
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
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
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
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