From: Logan Capaldo Date: 2006-12-05T09:13:06+09:00 Subject: Re: How do I catch a missing method on a passed block? On Tue, Dec 05, 2006 at 08:04:33AM +0900, J2M wrote: > I would like to invoke method_missing on baz in this code; > > x = Foo.new > x.bar { baz } > > I tried the following, and although the method is added to the proc > objects singleton class, the method_missing method never gets called. > Is there any way to achieve this? My current code (that doesn't work) > is below; > You'd want to define the method_missing <--- here x.bar { baz } Obviously that's not always practical, so what you need to do is change self. That can be acheived by using #instance_eval. This has disadvantages, obviously, but that's how to do it if you want to do it. Or you can pass a parameter into the block: x.bar { |q| q.baz } > class Foo > > def bar(*values, &block) > > if block_given? > > class << block > def method_missing(m, *args, &block) > puts "missing #{m}" > end > end > > block.call > > end > end > end >