using external API that has defined as special class where almost all standard methods are undefined to use for building xml. Where #method_missing
is responsible to generate elements based on the missing method name called over the object.
Basically in class body there is something to the effect of:
undef_method :send
Now I want to programatically call method by name. I guess I can use eval "obj.#{something}"
but I really don't like eval
.
I was thinking there must be some dark-side technique to revert the undefining of method #send
so I can alias it to #__send__
and undefine it again. That way I can happily call methods by name without eval. e.g. obj.__send__(:something, params)
.
So my question is how do I use monkey patching to revert the #send
method. I can't find anything to that effect. Even can't find anybody asking about it either.
UPDATE: My original problem was non-problem because there is a method #__send__
anyway, I knew only about #send
. The other part of the question was how to restore an undefined method. With help of @philomory asnwer here's what I've got:
[46] pry(#<CucuShift::DefaultWorld>)> class A
[46] pry(#<CucuShift::DefaultWorld>)* def gah
[46] pry(#<CucuShift::DefaultWorld>)* puts "gah"
[46] pry(#<CucuShift::DefaultWorld>)* end
[46] pry(#<CucuShift::DefaultWorld>)* end
=> :gah
[49] pry(#<CucuShift::DefaultWorld>)> class C < A
[49] pry(#<CucuShift::DefaultWorld>)* undef_method :gah
[49] pry(#<CucuShift::DefaultWorld>)* end
=> C
[50] pry(#<CucuShift::DefaultWorld>)> C.new.gah
NoMethodError: undefined method `gah' for #<C:0x000000070d7918>
[57] pry(#<CucuShift::DefaultWorld>)> class C
[57] pry(#<CucuShift::DefaultWorld>)* define_method :fff , A.instance_method(:gah)
[57] pry(#<CucuShift::DefaultWorld>)* end
=> :fff
[58] pry(#<CucuShift::DefaultWorld>)> C.new.fff
gah