Hunter Kelly wrote:
> Should this take a block, for completeness?
Good idea. I've changed it:
# Like super this calls methods from the inheritance chain which this
# method is replacing. However this version jumps over one or more
# methods in the inheritance chain. It is used like this:
#
# class X
# def it; p "X#it"; end
# end
#
# class Y < X
# def it; p "Y#it"; end
# end
#
# class Z < Y
# def it
# p "Z#it"
# superjump
# end
# end
#
# Z.new.it # outputs "X#it", "Z#it"
#
# Be careful. This method can't automatically pass the arguments of the
# caller like super does. You'll have to manually supply them.
def superjump(gap, *args, &block)
method_name = caller[0][/in `(.*?)'/, 1].intern
klass = self.class.ancestors[1 + gap]
klass.instance_method(method_name).bind(self).call(*args, &block)
end
Regards,
Florian Gross