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

(In words)

Date:07/10/06
Author:Daniel Straight
URL:n/a
Comments:1
Info:http://www.ruby-lang.org
Score: (3.02 in 40 votes)
#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

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

Comments

>>  Safalra said on 08/13/06 12:37:32

Safalra The code contains a spelling mistake - 'fourty' should be 'forty'. Clearly Ruby is an easier language than English...

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: