Hidetoshi NAGAI wrote:
> objs = @cars.collect{|owner, model|
>   obj = Object.new
>   obj.instance_eval{
>     @owner = owner
>     @model = model
>     def get_model
>       @model
>     end
>     def get_owner
>       @owner
>     end
>   }
>   obj
> }

Thanks. That's useful.

I wouldn't have imagined `def` inside instance_eval adds the method to 
the singleton.

BTW, anybody knows what exactly `def` does? Until now I thought it does 
this:

   Lookup the enclosing class (or module) and add the method
   to its m_tbl.

Based on Hidetoshi's code it seems `def` does this:

   Look at 'self'. If it's a class (or a module), add the method
   to its m_tbl. If it isn't, get its singleton and add the method
   to *its* m_tbl.

-- 
Posted via http://www.ruby-forum.com/.