Ruby Developer Question:

Do you know how Ruby looks up a method to invoke?

Tweet Share WhatsApp

Answer:

Since Ruby is a pure object-oriented language, it’s important to make sure your developer thoroughly understands how objects work. The first place that Ruby looks for a method is in the object’s metaclass or eigenclass—the class that contains methods directly defined on the object.

If the method cannot be found in an object’s metaclass, Ruby will then search for the method in the ancestors of an object’s class. The list of ancestors for any class starts with the class of the object itself, and climbs parent classes until it reaches the Object, Kernel, and BasicObject classes at the top of the Ruby class hierarchy.

If Ruby cannot find the method, it will internally send another method aptly called “method_missing?” to the object class. Ruby will repeat another search for this method, and will at least find it in the object class, provided the programmer did not see fit to define the “method_missing?” class earlier in the ancestry of the object.

Download Ruby Developer PDF Read All 74 Ruby Developer Questions
Previous QuestionNext Question
What is a class library in Ruby?Tell me how do you remove nil values in array using ruby?