From: Yossef Mendelssohn Date: 2007-09-06T09:41:28+09:00 Subject: Re: before, after and around Ruby 1.9 On Sep 5, 6:32 pm, Yukihiro Matsumoto wrote: > Hi, > > In message "Re: before, after and around Ruby 1.9" > on Thu, 6 Sep 2007 03:07:14 +0900, Trans writes: > > |Any chance Ruby 1.9 will have before, after and around method > |composition support? > > No. Wait for 2.0 for built-in method combination. The vague plan is > making open-class to stack methods on the current ones, unless > explicitly removed, i.e. > > class Foo < Object > def foo > puts "Foo#foo (1)" > end > end > class Foo # re-open > def foo > super # calls the first foo > puts "Foo#foo (2)" > end > end > > will print > > Foo#foo (1) > Foo#foo (2) > > No alias required. > > This works as "around". And "before" and "after" can be rewritten > using "around". Note that this is not a fixed idea at all. > > matz. Very interesting. Using 'super' in this way lets you easily have "around", and as you say, "before" and "after" can be written using "around" (such as your "after" example.) I like the idea, though it does confuse/overload 'super' a bit class Foo def foo 'foo' end end class Bar < Foo def foo super + 'bar' # results in 'foobar' end end class Bar < Foo def foo super + 'baz' # results in 'foobarbaz' end end One thing I'm not sure about is what happens with the original method if the class is re-opened and the new version doesn't use 'super' at all. I guess it sticks around without anything referring to it. Or there could be an optimization to actually replace the old method at that point. The other is if the new version takes different arguments class Foo def foo 'foo' end end class Foo def foo(caps = true) if caps super.upcase else super end end end I'm imagining some aliasing happens behind the scenes, so the new Foo#foo calls some Foo#old_foo and everything works out without errors. As you said, it's not a fixed idea at all. I was just running through some of the implications. -- -yossef