From: Avi Bryant Date: 2001-11-06T06:16:40+09:00 Subject: [ruby-talk:24427] Re: Detect future method calls in method_missing? What you need is sometimes called a "trampoline" - an object that simply "bounces" any messages it receives: class Trampoline def initialize(&b) @callback = b end def method_missing(meth, *args) @callback.call(meth, *args) end end Then you should be able to do: class MyClassWrapper def initialize @mc = MyClass.new end def method_missing(meth, *args) Trampoline.new do |next_meth, *next_args| @mc.content.send(next_meth, *next_args) end end end Will that work for you? On Tue, 6 Nov 2001, Morris, Chris wrote: > Given this (contrived example for a real wrapper I'm writing): > > class MyClass > attr_reader :content, :contentLength > def initialize > @content = 'test' > @contentLength = @content.length > end > end > > class MyClassWrapper > def initialize > @mc = MyClass.new > end > > def method_missing(methID, *args) > methodName = methID.id2name > @mc.content.send(methodName.intern, *args) > end > end > > Used this way: > > w = MyClassWrapper.new > puts w.reverse > puts w.content.length > > Yields: > > tset > ./default.method.in.wrapper.rb:16:in `send': undefined method `content' > for "test":String (NameError) > from ./default.method.in.wrapper.rb:16:in `method_missing' > from -:3 > > I understand why it does this. My question is this: is there a way, when > w.content.length is called, from within method_missing("content") to know > that ".length" will be coming next? (and in that case simply return > @mc.content). My guess is no, there's no way for method_missing to know what > future calls are coming down the pipe. > > Chris >