Search code examples
rubymonkeypatching

Is it theoretically safe to redefine any Ruby method that doesn't begin with underscores?


For example, is it theoretically safe to modify Object#object_id since there's always Object#__id__ if you really need to know what an object's id is?

Background: Curiosity piqued by What's another name for object_id?


Solution

  • In an ideal system where everything is perfectly documented and all people working with the code are aware of what's been re-defined and patched - then yes, maybe.

    But as we all know, such situations rarely exist. Personally, I feel that patching anything already defined in Kernel, Class, Module or Object is a no-no (unless you're doing it at a framework level.)

    Ultimately, I believe that Principle of Least Surprise should permeate coding decisions at all levels.