NoMethodError:Ruby ๐Ÿšซ

May 15, 2021

  4 minutes

Introduction

In Ruby, Everything is an object - this is the most common fact most of us rubyists are already familiar with. Despite it’s support for different programming paradims, including procedural, functional and object-oriented programming; it is mandatory to be familiar with the OO paradim to understand the various working mechanisms within ruby.

Object-Oriented Programming: the overview

We can find infinite contents on the web explaining the components namely classes & objects; and the foundational principles of object oriented programming namely Encapsulation,Abstraction,Inheritence and Polymorphism. In OOP, a class is a template/blueprint for creating objects and can have various attributes(variables) and methods(functions). Similarly an object is a real world entity/simply an instance of a class . An object has its own set of attributes which defines its Local state.

In ruby everything being an object, The class is also an object; sounds tricky, doesn’t it? So how does the entire process work? Let’s dive further.

The communication between objects happen through passage of messages. Following are the characteristics of a ruby object:

  • The object has a local state (set of attributes). The local state in ruby is eventually a collection of objects; everything in ruby being an object.
  • The object responds to messages.

Let’s look at an example below:

name = "niraj" //name is an object of special type String
// the name object responds to different messages. eg: upcase, length, split('') etc.

name.length 
// length message is being sent to String object => returns 5

name.split('') 
// split message is sent to String object passing in empty string '' arguments
//returns ['n', 'i', 'r', 'a', 'j']

We can therefore define an object as A collection of references to other objects(local state) that responds to certain messages.

One more concept before we go to NoMethodError.

Singleton class

In ruby, every object has its own individual class called Singleton Class (also called metaclass or eigenclass). Singleton classes are useful in allowing the individual objects to extend its properties and methods without affecting other objects of the same class. When an object extends a module, all the modules are added to that object’s singleton class.

Class Hierarchy

Let us consider a custom class for example. There is this class called Animal; And we created an object human of the class Animal.

The object human is gonna have a class of its own which is its singelton class. Every user defined classes in ruby implictly extend from a superclass called Object. The class Object has its own superclass called BasicObject which sits on the top of hierarchy. BasicObject class doesn’t have anu super class. Let us summarize this point below.

  • BasicObject class sits on top of hierarchy. A class called Object extends from it.
  • Every top level user-defined class extends from Object class. The others can extend from sub-classes of User defined classes as well.
  • Every object has its own class called Singleton class.

Ruby Object Hierarchy

NoMethodError. So what actually is it?

When a method is called upon a ruby object; the following things happens in the given particular order:

  1. It looks for the method in Object's Singleton Class.
  2. If the method is not found in Object’s Singleton Class, it looks for the method in Object's Class. Object’s Class and class Object are two different things.
  3. If the method isn’t found in the Object’s Class, it looks for the method in the Object's Class Super Class until it reaaches the BasicObject Class.
  4. Wondering what happens if the method is found in either of the above classes? It simply executes the method ๐Ÿ˜ƒ.
  5. If the method is not defined until BasicObject Class, it goes back to the Object’s Singleton Class and looks for a special method called method_missing; which is going to be a separate topic for the future.
  6. If method_missing isn’t found in the Object’s Singleton Class; it moves up the class hierarchy upto the BasicObject Class searching for method_missing.
  7. Method_missing is a special method with it’s own definition; to be defined by the user. If method_missing is found, the steps within it gets executed; simple.
  8. If method_missing isn’t found upto the BasicObject class; NoMethodError is raised !!! ๐Ÿšซ๐Ÿšซ๐Ÿšซ

Hope you guys are clear about Ruby’s NoMethodError now. Enjoy ๐Ÿ‘๐Ÿ‘.


comments powered by Disqus