From: ara.t.howard@... Date: 2006-12-06T02:33:24+09:00 Subject: Re: How do I catch a missing method on a passed block? On Tue, 5 Dec 2006, J2M wrote: > ara.t.howard@noaa.gov wrote: > >> but block __will__ respond_to? 'call' - ergo your method_missing hook will >> never fire? what are you trying to do? > > I want to catch a missing method (baz) inside the passed block. here's how to catch it: harp:~ > cat a.rb class Foo def bar *a, &b if b singleton_class = class << b; self; end singleton_class.module_eval{ def method_missing m, *a, &b p [m, a, b] end } b.instance_eval &b end end end Foo.new.bar{ baz } harp:~ > ruby a.rb [:baz, [], nil] and here's how to forward it to another object, in this case the Foo object: harp:~ > cat a.rb class Foo def bar *a, &b if b this = self singleton_class = class << b; self; end singleton_class.module_eval{ @@this = this def method_missing m, *a, &b @@this.send m, *a, &b end } b.instance_eval &b end end def baz p 'baz' end end Foo.new.bar{ baz } harp:~ > ruby a.rb "baz" there are many ways to skin that cat. can you start out by just telling us __exactly__ what you want are trying to do? the various ways of solving this each have consequences which could more easily be chosen from if we knew. kind regards. -a -- if you want others to be happy, practice compassion. if you want to be happy, practice compassion. -- the dalai lama