Mikael Brockman <phubuh / phubuh.org> writes:

> Here's my take on prototype-based programming:
>
> | def add_method (id, &closure)
> |   (class << self; self; end).send(:define_method, id, &closure)
> | end
>
> And an example:
>
> | foo = [1, 2, 3]
> | foo.add_method :frob do |x|
> |   self[x] * self.length
> | end
> | 
> | # foo.frob 1 => 6

I just took that verbatim from a utility file of mine, forgetting what
its actual purpose is.

The difference between Object#add_method and class << foo is that the
former takes a closure, so it can use variables from its lexical scope.

  mikael