I’m making a weird class where I want to catch every method sent to an object of the class. I can achieve most of what I want with method_missing e.g.
class MyClass
def method_missing m, *args
# do stuff
end
end
The problem then is all of the instance methods that MyClass inherits from Object. I could go through each method one-by-one and redefine them, but I was hoping for a more flexible approach. All of the metaprogramming methods I’ve tried have complained with a NameError when I try to touch those instance methods.
As noted in the comments and the answer from @DanCheail, if you’re using ruby 1.9+ you should use a
BasicObjectrather than undefining methods ofObject. At the time of this posting usage of 1.8.x was still quite prevalent, even though 1.9 had been released for some time.Rather than attempting to “redefine” anything, you could wrap your object in a blanked (for the most part) proxy object, something like: