This is due to a kind of hack at the top level of Ruby. Have you ever wondered how this works?
def foo end p self foo class Bar def test p self foo end end Bar.new.test
How can two completely different objects ( main and a Bar ) call foo , like calling a private method? The reason is ... it's a private method call.
When you define a method at the top level of your Ruby script, it is included (via Object ) in each object. This is why you can call top-level methods, such as global functions.
But why does this violate only hash and not other common methods? def to_s;end , for example, will not be split into to_s . The reason is that hash is recursive: most class implementations end up referencing Object#hash for their implementations. By overriding this base case, you break it around the world. For other methods, such as to_s , you will not see a global change because it approaches the inheritance chain and is not called.
* the only objects that do not violate this are a few literals, which probably have hardcoded hash values, for example. [] {} "" true , etc.
source share