From: nickb@... (Nick Bensema) Date: 2001-02-28T08:40:04+09:00 Subject: [ruby-talk:11724] Re: Bottles of Beer finally in Ruby I think I can make one that better expresses the song. Well, maybe. Actually, I just decided to encapsulate the singing. Perhaps a Crowd class could be added so that the beer could be passed to them, but considering they get little consideration in the song, maybe we Aren't Gonna Need It. ---- # BeerShelf class, encapsulates all beer. class BeerShelf attr_reader :stock def initialize( bottles ) @stock = bottles end def take_one_down @stock -= 1 if @stock > 0 end def inventory if got_beer? "#{@stock} bottle" + ((@stock==1) ? "" : "s") + " of beer" else "No more bottles of beer" end end def got_beer? @stock > 0 end end # Singer class, encapsulates all singing. class Singer def call_inventory(shelf) puts "#{shelf.inventory} on the wall" end def take_one_down_from(shelf) shelf.take_one_down puts "Take one down, pass it around..." end def sing_about(shelf) while shelf.got_beer? 2.times { call_inventory(shelf) } take_one_down_from(shelf) call_inventory(shelf) puts end end end Singer.new.sing_about(BeerShelf.new(100)) -- Nick Bensema ICQ#2135445 ==== ======= ============== AVOID MISSING BALL TO SCORE