From: Robert Klemme Date: 2013-03-19T03:11:17+09:00 Subject: Re: Finding one's way with 'super' in define_method/alias_emthod On Mon, Mar 18, 2013 at 5:50 PM, Marcin Rzeźnicki wrote: > Hi all! > This is my first post here, so please don't yell at me too harshly :-) Welcome to the wonderful world of Ruby! > I have a little problem with 'super' keyword when it is used in > define_method/alias_method. First, a little example: > > class C > def greetings > p "Hello, #{caller.first}" > end > def curses > p "Damn you, #{caller.first}" > end > end > > class D < C > def greetings > super > end > define_method :curses, instance_method(:greetings) > end > > [3] pry(main)> D.new.greetings > "Hello, (pry):11:in `greetings'" > => "Hello, (pry):11:in `greetings'" > [4] pry(main)> D.new.curses > "Hello, (pry):11:in `greetings'" > => "Hello, (pry):11:in `greetings'" > > So, as you can see 'super' is bound, perhaps a little unexpectedly, to > a method instance rather than to its call site. Hmm, I'd still say it's the call site since the call site sits in method D#greeting even though the method is also known as D#curse. > You can see it clearly > when inspecting the caller - in both cases it is 'greetings'. While > this may seem logical and sound, it is quite troubling when you try to > make a wrapper method around user-defined method doing metaprogramming > tricks. > Do you know any workaround? How can I overcome this problem and let > super be bound correctly if used from aliased method? Thanks in > advance for your opinions. I am not sure what you expect should be called "bound correctly". Basically what you did is you said D#curse should be D#greeting (either via define_method as shown above or alias). That means, D's method #curse behaves like D's #greeting and hence ultimately invokes C#greeting and not C#curse. Btw, somehow your chain example seems incomplete: is #chain a method defined in Module? What about method names? I would have expected the chained method name to be given as argument. I once coded something together for wrapping methods - you may find it in the archives. I am not sure though that I still have the code elsewhere... I only found this one: https://gist.github.com/rklemme/3002732 but that's not the one I mean. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/