Language Ruby
(wall-based OO version)
Date: | 07/07/05 |
Author: | Kevin Baird |
URL: | n/a |
Comments: | 2 |
Info: | http://ruby-lang.org |
Score: | (2.95 in 19 votes) |
#!/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 | |
object-oriented version | Mike Gertz | 04/20/05 | 2 | |
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 | |
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
AEDE said on 10/28/05 14:51:31
smart but not very ruby oriented...
looks like C++ to me...
Brad said on 03/08/08 22:16:30
This looks very clean to me. I'm looking for an easy to grok scripting language and something that slides into OO this easy is to be commended. It doesn't look like C++ to me (nor Java for that matter.) It looks a bit easier/simpler than trying the same in Python.
This has been an extremely useful example to me.