I’m writing some code that looks like this:
class SomeClass def method_to_wrap(arg) ... return a_thing end end module WrapperModule def method_to_wrap(arg) cloned = self.modifyAndCloneSelf(foobar) #this is what I want to do, but does not work a_thing = cloned.super(arg) #uses cloned as the bound object, and passes arg return a_thing end end SomeClass.prepend(WrapperModule)
The idea is to call modifyAndCloneSelf() to modify the object and then using the modified object to call the original method_to_wrap in that object. I know I can do something like
super_method = self.class.instance_method(:method_to_wrap).super_method a_thing = super_method.bind(cloned).call(arg)
but this seems to be a lot slower than using a super call (almost 2x slower) and is not entirely correct (it gets the instance method of the lowest ancestor, not the method above WrapperModule).
Also I can’t modify the modifyAndCloneSelf() method, it’s in a library and has to return a cloned object (its a Mongoid::Criteria method)