This is a theoretical question. I know that I can call a method by using ::. It is rarely used, but possible. For example:
user = User.new
user::name
The question is: How it is working under the hood? What are the implementation details allowing us to do user::name?
I found some places where this way of calling method is mentioned like:
but they do not provide the implementation details, which for me is interesting topic. I also tried to go through the Ruby source code, but for now without any success.
I also checked those threads:
- . vs :: (dot vs. double-colon) for calling a method
- What is Ruby's double-colon
::? - What does :: (double colon) mean in Ruby?
- Ruby's double colon (::) operator usage differences
- Double colons before class names in Ruby?
- Double colons in Ruby's class definition
- What does constant prefixed with double colons mean?
- Idiomatic use of double-colon (double-column, or ::) syntax for Ruby methods
- difference between dot(.) and double colon (::) in accessing class method
They are explaining how :: works and differences between :: and .. And this is not what I search for.
I would appreciate any information that can help me to learn more about :: to understand it deeply. My question is about how the :: works inside, not how to use it. I would like to get to know implementation details for ::.
In Ruby, the double colons :: are used as a namespace resolution operator to access constants, classes, or modules within a module or class. When it comes to method calls, the double colons are used in the context of class or module methods, not instance methods.
Here's how the double colons work under the hood in case you use them for method calls:
Constant/Class/Module Lookup: When you use Module::Class or Module::CONSTANT, Ruby searches for the specified class, module, or constant within the given module's namespace. It searches first in the module itself, then looks up the ancestor chain.
Module/Class Method Calls: When you use Module::method or Class::method, you are calling a method that is defined on the module or class itself, not on instances of that module or class.
Under the hood, Ruby is looking up the method within the class/module's method table. This is essentially a hash-like structure that maps method names to their corresponding method implementations. The method lookup starts from the most specific class/module and goes up the ancestor chain if the method is not found in the current class/module.
This process is a fundamental part of Ruby's object-oriented nature and its inheritance and method resolution mechanisms.
Here's a simplified example to illustrate how it works:
In this example, both MyModule and MyClass have a class/module method named my_method. Using the double colons to call these methods directly accesses them from the corresponding class/module.
Remember that the double colons primarily pertain to constants, classes, modules, and their associated methods, not instance methods. For instance methods, you typically use dot notation on instances of classes.