On Thu, Mar 27, 2003 at 09:07:47PM +0900, ts wrote:
> >>>>> "B" == Brian Candler <B.Candler / pobox.com> writes:
> 
> B>     self.class.instance_eval { define_method(:pp, pp) }
> 
>        class << self; self end
> 
>  give you the singleton class

That's fine, but then how do I define the method in the singleton class
rather than in the parent class? I can't use 'def' because the local
variable pp drops out of scope.

class Foo
  def addmeth(m,x)
    self.class.instance_eval { define_method(m,x) }
  end
end

a = Foo.new
b = Foo.new

class <<a; end

pp = proc { |x| x+1 }
a.addmeth(:pp,pp)
p a.pp(10)   # >> 11
p b.pp(10)   # >> 11   (Oops, I didn't want that)

Regards,

Brian.