# Interception method\_added for blocks

**URL:** <https://www.ruby-forum.com/t/interception-method-added-for-blocks/50060>\
**Category:** Ruby\
**Created:** [December 12, 2005, 6:40pm UTC](https://www.ruby-forum.com/t/interception-method-added-for-blocks/50060 "2005-12-12T18:40:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@robertj](https://www.ruby-forum.com/u/robertj)\
**Post date:** [December 12, 2005, 6:40pm UTC](https://www.ruby-forum.com/t/interception-method-added-for-blocks/50060/1 "2005-12-12T18:40:01Z")

</div>

hi,

is it possible to somehow intercept method\_added  
for defs defined within a block?

y = Proc.new {  
def x  
end  
}

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

ciao robertj

---

<div class="post-metadata">

**Author:** ![robertj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@robertj](https://www.ruby-forum.com/u/robertj)\
**Post date:** [December 12, 2005, 7:34pm UTC](https://www.ruby-forum.com/t/interception-method-added-for-blocks/50060/2 "2005-12-12T19:34:12Z")

</div>

class \<\< Proc  
alias\_method :\_new, :new  
def new( \*args, &blk )  
puts “Intercept…”  
\_new( \*args, &blk )  
end  
end

You can’t do it with method\_added becuase it is a _post_ hook --the  
method has already been created.

---

<div class="post-metadata">

**Author:** ![robertj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@robertj](https://www.ruby-forum.com/u/robertj)\
**Post date:** [December 12, 2005, 7:40pm UTC](https://www.ruby-forum.com/t/interception-method-added-for-blocks/50060/3 "2005-12-12T19:40:13Z")

</div>

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 = Proc.new{ def x; end }  
=\> #Proc:0xb79d553c@:11(irb)  
irb(main):012:0\> A.class\_eval &d  
“A#x”  
=\> nil  
irb(main):013:0\> B.class\_eval &d  
“B#x”  
=\> nil
