From: Michael Jackson Date: 2010-04-30T05:19:38+09:00 Subject: Hijacking `super' I know the following is probably impossible, but I just thought I'd ping the list to see if any adventurous meta programmer knows of a way to essentially change the binding of a block. Here's essentially what I'm trying to do: $ cat super.rb class A def call "A" end end class B < A def call(&b) instance_eval(&b) end end puts B.new.call { super } $ ruby super.rb super.rb:13:in `block in
': super called outside of method (NoMethodError) from super.rb:9:in `instance_eval' from super.rb:9:in `call' from super.rb:13:in `
' Now, when `super' is used inside the block it tries to call a method with the same name as the method that it's used in, but in the parent class. Obviously, since the block is created outside of any method here Ruby complains that super was called outside of a method. However, if you change the code to use a string and plain old `eval', you can mimic the desired behavior. $ cat super1.rb class A def call(s) "A" end end class B < A def call(s) eval(s) end end puts B.new.call 'super' $ ruby super1.rb A The only problem now is that you're limited to using strings and you can't use blocks anymore. The conclusion I've come to is that you essentially need to be able to alter the binding of the given block within `call' to be the binding that exists within the `call' method. Then, super should be able to find the method that it's called in and the superclass. However, unlike most other things in Ruby, bindings are pretty opaque. Any ideas are very much appreciated. Michael -- Michael Jackson http://mjijackson.com @mjijackson