On Saturday 11 September 2004 05:48 am, Hal Fulton wrote:
> OK, I have this variable called box.
>
> I want to define a singleton method xy on it. No problem.
>
> However, I need a closure. So I need to use define_method
> rather than def.
>
> This is how I did it:
>
>    class << box; self; end.class_eval { define_method(:xy) {[x,y]} }
>
> Surely there is an easier way??

Off the top of my head I use a common lib:

module Kernal

  def metaclass
    (class << self; self; end)
  end

  def metaclass_eval(&block)
    (class << self; self; end).class_eval(&block)
  end

  def define_singleton(meth, &block)
    metaclass_eval { define_method(meth, &block) }
  end

end


T.

-- 
( o _
 //    trans.
/ \    transami / runbox.com

I don't give a damn for a man that can only spell a word one way.
-Mark Twain