Method Lookup in Rails

Method Lookup in Rails ::

Link ::  https://www.honeybadger.io/blog/ruby-method-lookup/

The process (or path) Ruby follows in figuring this out is what we call method lookup. Ruby has to find where the method was created so that it can call it. It has to search in the following places to ensure it calls the right method:

......................................................

1. Singleton methods

2. Methods in mixed-in modules

3. Instance methods

4. Parent class methods or modules

5. Object, Kernel, and BasicObject

......................................................


  1. Singleton methods: Ruby provides a way for an object to define its own methods; these methods are only available to that object and cannot be accessed by an instance of the object.
  2. Methods in mixed-in modules: Modules can be mixed into a class using prependinclude, or extend. When this happens, the class has access to the methods defined in the modules, and Ruby goes into the modules to search for the method that has been called. It's also important to know that other modules can be mixed into the initial modules, and the search also progresses into these.
  3. Instance methods: These are methods defined in the class and accessible by instances of that class.
  4. Parent class methods or modules: If the class happens to be a child of another class, Ruby searches in the parent class. The search goes into the parent class singleton methods, mixed modules, and its parent class.
  5. Object, Kernel, and BasicObject: These are the last places where Ruby searches. This is because every object in Ruby has these as part of their ancestors.
........................................................................


Comments

Popular posts from this blog

Rails 7 Features :: Comparison with Rails 6 and Rails 5