¿Eres nuevo? ¡Lee el FAQ y ponte al día!
185 visitas

99 Bottles of Beer in Ruby

This is a ruby implementation of the typical example 99 bottles of beer.

Ruby
  1. # 99 Bottles of beer, in Ruby
  2. # By Victor Borja, Sep 14, 2006
  3. # This one shows my favorite Ruby features:
  4. #   continuations, open classes, singleton classes, blocks and being funny!
  5.  
  6. class Integer # The bottles
  7.   def drink; self - 1; end
  8. end
  9.  
  10. class << song = nil
  11.   attr_accessor :wall
  12.  
  13.   def bottles
  14.     (@bottles.zero? ? "no more" : @bottles).to_s <<
  15.       " bottle" << ("s" unless @bottles == 1).to_s
  16.   end
  17.  
  18.   def of(bottles)
  19.     @bottles = bottles
  20.     (class << self; self; end).module_eval do
  21.       define_method(:buy) { bottles }
  22.     end
  23.     self
  24.   end
  25.  
  26.   def sing(&step)
  27.     puts "#{bottles.capitalize} of beer on the wall, #{bottles} of beer."
  28.     if @bottles.zero?
  29.       print "Go to the store buy some more, "
  30.       step = method :buy
  31.     else
  32.       print "Take one down and pass it around, "
  33.     end
  34.     @bottles = step[@bottles]
  35.     puts "#{bottles} of beer on the wall."
  36.     puts "" or wall.call unless step.kind_of? Method
  37.   end
  38.  
  39. end
  40.  
  41. callcc { |song.wall| song.of(99) }.sing { |beer| beer.drink }
  42.  

­

I found it here.

Etiquetas: Ruby 99 Bottles of Beer

Insertar: