From: "Jesús Gabriel y Galán" Date: 2011-04-15T16:45:35+09:00 Subject: Re: looking for an "inversion" pattern On Fri, Apr 15, 2011 at 7:16 AM, Fearless Fool wrote: > I'm sure there's a clean way to do this in Ruby, but I haven't figured > it out. > > I'd like to create a method +foo+ that transforms: > >   my_obj.foo.some_method(*args) > > to > >  MyClass.some_method(my_obj, *args) > > Maybe I'm overthinking this.  I'll keep working on this, but in the > meantime, I'm open to suggestions. I would create a method foo that returns a proxy that remembers which was the original receiver. This proxy can have a method_missing implementation that calls MyClass.xxxx passing the original receiver and all params. For example: ruby-1.8.7-p334 :003 > class Proxy ruby-1.8.7-p334 :004?> def initialize target ruby-1.8.7-p334 :005?> @target = target ruby-1.8.7-p334 :006?> end ruby-1.8.7-p334 :007?> def method_missing meth, *args, &blk ruby-1.8.7-p334 :008?> MyClass.send(meth,@target,*args,&blk) ruby-1.8.7-p334 :009?> end ruby-1.8.7-p334 :010?> end => nil ruby-1.8.7-p334 :011 > class Something ruby-1.8.7-p334 :012?> def foo ruby-1.8.7-p334 :013?> Proxy.new self ruby-1.8.7-p334 :014?> end ruby-1.8.7-p334 :015?> end => nil ruby-1.8.7-p334 :016 > class MyClass ruby-1.8.7-p334 :017?> def self.test1 obj, *args ruby-1.8.7-p334 :018?> p [obj,*args] ruby-1.8.7-p334 :019?> end ruby-1.8.7-p334 :020?> end => nil ruby-1.8.7-p334 :021 > my_obj = Something.new => # ruby-1.8.7-p334 :022 > my_obj.foo.test1 1,2,3 [#, 1, 2, 3] I don't know if method foo is part of your class, or maybe you can build a method and extend the required objects with it. Jesus.