Bernhard 'elven' Stoeckner schrieb:
> thoran / thoran.com scribbled on Sunday 19 Mar 2006 09:33:
>> [..]
> 
> module RandomMethod
>         def random_method
>                 m = self.methods - Object.new.methods -
>                         Object.methods - ["random_method"]
>                 m = m.collect {|n|
>                         self.method(n).arity == 0 ? self.method(n) : nil
>                 }.compact
> 
>                 m[rand(m.size)].call() if m.size > 0
>         end
> end


you call() on a string rather than on the object.
Improved version with 'usecase' :


module RandomMethod
  def random_method
    m = methods - Object.new.methods -
      Object.methods - ["random_method"]

    m.reject{|n| method(n).arity.nonzero?}
    method(m[rand(m.size)]).call() if m.size > 0
  end
end

class Dice
  include RandomMethod
  def one; 1;end
  def two; 2;end
  def three; 3;end
  def four; 4;end
  def five; 5;end
  def six; 6;end
end

dice = Dice.new
10.times{puts dice.random_method}

rotfl

Simon