Language Ruby
(wall-based OO version)
| Date: | 07/07/05 |
| Author: | Kevin Baird |
| URL: | n/a |
| Comments: | 2 |
| Info: | http://ruby-lang.org |
| Score: |
#!/usr/bin/env ruby
# 99 bottles problem in Ruby
class Wall
def initialize(num_of_bottles)
@bottles = num_of_bottles
end
def sing_1_verse
@output = sing_num(@bottles) + " on the wall, " + sing_num(@bottles) + "\n"
@output += "take one down, pass it around, " + sing_num(@bottles-1) + "\n\n"
return @output
end
def sing_all
@output = ''
while @bottles > 0 do
@output += sing_1_verse()
@bottles -= 1
end
return @output
end
def sing_num(num)
@counter = (num > 1) ? 'bottles' : 'bottle'
"#{num} #{@counter} of beer"
end
end # class Wall
wall = Wall.new(99)
puts wall.sing_all()
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 | |
| In words | Daniel Straight | 07/10/06 | 1 | |
| object-oriented version | Mike Gertz | 04/20/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
looks like C++ to me...
This has been an extremely useful example to me.