Ruby Shelter Against Overriding

I just found out that in Java there is a difference between overriding and hiding (static methods are hidden, not overly), which implies that Java uses both early and late bindings.

Is there something like hiding a method, or does it just have a method override?

+5
source share
1 answer

Java has three different types of “methods”: instance methods, static methods, and constructors. Ruby has only one: instance methods.

In Java, static methods must behave differently than instance methods, because classes are not objects. They do not have a class, therefore there is no superclass, therefore there is nothing to overestimate. In Ruby, classes are objects like any other object, they have a class that can have a superclass, and therefore subclasses can override the methods of a superclass.

Note. You may have heard of class methods or Singleton methods in Ruby. It's a lie. Well, don't lie. This is a convenient shorthand that we use because the "class method" is easier to pronounce than the "ordinary instance method of the Singleton class class" ... but that’s exactly what it is. There are no class methods.

In Ruby, each object can have its own methods. They are called "single point methods." Classes are objects like any other object, so they can also have solid methods. When the object that the singleton method is is a class, we call this method a class method. But this is what we call, there is no difference between a class method and a singleton method.

In fact, in Ruby, every object has a singleton class. The singleton class is in a 1: 1 ratio with the object: the object has exactly one singleton class, and each singleton class has exactly one instance, its object. So, when I said above that objects can have methods, and these methods are called single point methods? Well, that too is a lie. Singleton methods are truly standard instance methods that, as it turns out, are defined in a singleton class of an object and therefore can only be called on this object (since this object is the only instance of its singleton class).

So, when a method is defined in a singleton class, we call it a single-point method, and when a singleton class belongs to a class, we call it a class method, but all these are just instance methods. (BTW: modules work the same. In this case, they are called modular methods or sometimes "module functions.")

The class pointer of an object always points to its singleton class. The actual class of the object is the superclass singleton class, i.e. The singleton class superclass points to the actual class of the object. (If there are no mixins that become superclasses of the class into which they are mixed, so if you mix the module into a singleton class, the module becomes the superclass of the singleton class, and the old superclass becomes the superclass of the module, or rather the proxy class includes it.)

This means that finding the method that is the most frequently performed operation in the OO language becomes very simple and very fast: grab the object, take its class pointer, see if there is a method, take the superclass pointer, see if there is a method, take a pointer superclass ... until you find a method.

This means that reflection is getting a little more complicated, but reflection is not a critical operation. For example, if you request an object for your class, you cannot simply return the class pointer, as this will always be its singleton class and, therefore, not very informative. You should get a superclass, a superclass, and a superclass, and so on, until you end up in a class that is not a single class or an included proxy class.

But finding a method is very simple, and super always does what you expect.

In particular, when you create a new class, the singleton class of the superclass becomes the superclass of the singleton class of the subclass, so the "class methods" are inherited as you would expect.

So, to repeat: while Java has three different types of “methods” with different inheritance behavior (instance methods are inherited, static methods are not, constructors are inherited, but have this super restriction), Ruby has only one. However, it has three different classes: ordinary classes, singleton classes and includes proxy classes (created as proxies for mixins when mixing a module into a class). The latter two are also called “virtual classes” inside YARV, the most widely used implementation of Ruby.

Last: There are also so-called "global methods", sometimes called "global procedures" or "global functions." Again, as you probably already guessed, this does not exist. When you define a method outside of any class, it implicitly becomes a private instance of the Object method and, therefore, is available for each object.

[I ignored two things here: BasicObject and prepend . These complications matter, especially the latter. But the basic mental model remains.]

+10
source

All Articles