Forum: Ruby interception method_added for blocks

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
robertj (Guest)
on 2005-12-12 19:40
(Received via mailing list)

is it possible to somehow intercept method_added
for defs defined within a block?

y = {
    def x

for example in the code above who gets notfied of the existance of x?

ciao robertj
Trans (Guest)
on 2005-12-12 20:34
(Received via mailing list)
class << Proc
    alias_method :_new, :new
    def new( *args, &blk )
      puts "Intercept..."
      _new( *args, &blk )

You can't do it with method_added becuase it is a *post* hook --the
method has already been created.
Trans (Guest)
on 2005-12-12 20:40
(Received via mailing list)
Ah misread that a bit. The code won't help, so ignore that.

Actual answer: It depends on where you evaluate the code.

irb(main):001:0> class A
irb(main):002:1>   def self.method_added( sym )
irb(main):003:2>     p "A##{sym}"
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0> class B
irb(main):007:1>   def self.method_added( sym )
irb(main):008:2>     p "B##{sym}"
irb(main):009:2>   end
irb(main):010:1> end
=> nil
irb(main):011:0> d ={  def x; end }
=> #<Proc:0xb79d553c@(irb):11>
irb(main):012:0> A.class_eval &d
=> nil
irb(main):013:0> B.class_eval &d
=> nil
This topic is locked and can not be replied to.