Language Ruby
(In words)
| Date: | 07/10/06 |
| Author: | Daniel Straight |
| URL: | n/a |
| Comments: | 1 |
| Info: | http://www.ruby-lang.org |
| Score: |
#99 Bottles of Beer on the Wall
#by Daniel Straight, July 10, 2006
#see http://ruby-lang.org/en/ for info on Ruby
#This version adds a to_w (to word) conversion to the Integer class
#and then uses it to print the lyrics with words instead of numerals
class Integer
def to_w
words = {
0 => "no",
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
7 => "seven",
8 => "eight",
9 => "nine",
10 => "ten",
11 => "eleven",
12 => "twelve",
13 => "thirteen",
14 => "fourteen",
15 => "fifteen",
16 => "sixteen",
17 => "seventeen",
18 => "eighteen",
19 => "nineteen",
20 => "twenty",
30 => "thirty",
40 => "fourty",
50 => "fifty",
60 => "sixty",
70 => "seventy",
80 => "eighty",
90 => "ninety"}
if self < 21
words[self].capitalize
else
if self.to_s =~ /\d0/
words[self].capitalize
else
"#{words[(self.to_s.slice(0,1).to_i*10)].capitalize}-#{words[(self.to_s.slice(1,2).to_i)]}"
end
end
end
end
def bottles (num)
if num >= 2 or num == 0
"#{num.to_w} bottles"
else
"#{1.to_w} bottle"
end
end
def sing (verse)
puts "#{bottles(verse)} of beer on the wall"
puts "#{bottles(verse)} of beer"
if verse > 0
puts "Take one down; pass it around"
puts "#{bottles(verse-1)} of beer of wall"
elsif verse == 0
puts "Go to the store, buy some more."
puts "#{bottles(99)} bottles of beer on the wall"
end
puts
end
99.downto(0) do |i|
sing i
end
Download Source | Write Comment
Alternative Versions
| Version | Author | Date | Comments | Rate |
|---|---|---|---|---|
| Using continuations, singleton classes | Victor Borja | 09/15/06 | 9 | |
| minimal version | Anonymous | 05/18/05 | 3 | |
| shows inheritance, iterators, yield, etc | Kian Wright | 06/10/05 | 0 | |
| alternative version | Greg T. | 05/18/05 | 10 | |
| object-oriented version | Mike Gertz | 04/20/05 | 2 | |
| wall-based OO version | Kevin Baird | 07/07/05 | 2 | |
| monkeypatch and anonymous procs | J. B. Rainsberger | 04/04/07 | 0 | |
| Readably re-opening Integer, teetotaller | Eric Budd | 01/06/08 | 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