On 15 Jan 2008, at 19:33, ara.t.howard wrote:
> i personally avoid aliases like that - they stack when you double  
> require or double include a module and throw into a loop.  this  
> kind of thing can be done safely and generically using a variable  
> for the previous method that's protected from the gc and a lookup  
> in the new method:

I see.  That sounds much more sensible.

> cfp:~ > cat a.rb
> class A
>   def foo
>     p "A.foo"
>   end
> end
> class B
> end
>
> module M
>   NoGC = []
>
>   def self.included other
>     other.module_eval do
>       if((foo = instance_method 'foo' rescue false))
>         NoGC.push foo
>         supra = "ObjectSpace._id2ref(#{ foo.object_id }).bind 
> (self).call(*a, &b)"
>       end
>       eval <<-code
>         def foo *a, &b
>           #{ supra }
>           p "M.foo"
>         end
>       code
>     end
>   end
> end
>
> A.send :include, M
> B.send :include, M
>
> A.new.foo
> B.new.foo
>
>
> cfp:~ > ruby a.rb
> "A.foo"
> "M.foo"
> "M.foo"
>
>
> this allows you to both override and super up, in any combination,  
> with a method injected late into a class hierarchy

Wonderful!

I should confess that I haven't entirely convinced myself that I  
understand your construction of supra.  Given that you have the  
unbound method in the foo variable, is the reason why you can't bind  
foo directly, perhaps like this:

     supra = "#{foo}.bind(self).call(*a, &b)"

...because there's no way to write the string so it eval's the way we  
want it to?

Thank you for the enlightenment :)

Regards,
Andy Stewart

-------
http://airbladesoftware.com