Method lookup and execution in Ruby

Wanna know how Ruby finds and executes a method?

Shiva Bhusal
Jun 26, 2017 · 2 min read

Method Introduction

First thing first, let know about methods. What are methods, I say. Ruby is a pure OO language. Meaning, everything is an object; even methods are.

The core philosophy of Object orientation is “message passing”.

OOP to me means only messaging,. — Alan Kay

So, basically the methods are the messages you pass to objects.

Method definition in compile time

When class definition code (class [ClassName]) is encountered, Ruby instantiates an object of Class class with name ClassName you supplied. And, the methods you defined will be defined inside the ClassName definition. i.e. methods will be in the class’s blueprint. You can later pluck methods if you wish.

@instancevariables however are store in the object they belong to. And, only defined when the defining method is invoked. i.e. if constructor function is meant to define an @instance_var then the var will be defined during construction; otherwise won’t.

Method lookup

In the example above, when you invoke cow.speak then Ruby will search the cow object for singleton methods called speak , if not found will search the Cow class for the speak method. If still its not there then search Printable module because its in the superclass / ancestors chain. It continues to bubble up till it finds the method.

MySubclass.ancestors # => [MySubclass, MyClass, Object, Kernel, BasicObject]

In OO terms, basically its searching if this object is supposed to respond to that particular message; If yes, then how?

If the object is not able to respond, then BasicObject#method_missing method is invoked. You can redefine what to do in that case for your object.

Method lookup model in Ruby

If the same method is defined in child and parent both, then one lower in hierarchy has higher priority. Means, it tries to return from search as soon as possible. If you wish to invoke method defined in parent then use super method.

Method Execution

Executing the method found is little bit tricky. Found method might have mentions to @instance variables. And, you know that @instance vars are store in the objects. So, Ruby makes sure that the methods are executed in the Object’s binding.

Shiva Bhusal

Written by

Electronics & Software Engineer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade