Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Instance methods
3.
Singleton methods
4.
Eigenclass
5.
Frequently asked questions
5.1.
Are there class methods in Ruby?
5.2.
Define singleton methods?
5.3.
What is the self keyword?
5.4.
How many ways we can define class methods in ruby?
5.5.
What differentiates a class from a module?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Singleton Methods and the Eigenclass in ruby

Introduction

In this article, we are going to learn what are singleton methods and Eigneclass in ruby and how and where we can implement both of them in our code. But before diving into that, let's first have some brief about the Ruby and class and module just to have a better understanding.

ruby introduction

Ruby is a dynamic and purely object-oriented programming language that was developed by Yukihiro Matsumoto. It is a high-level programming language with much easier syntax so that it can be learned easily. 

Now, what is a class in Ruby? A class is like a template for the program code that can be accessed through an instance object of the class. We can implement various methods in a class according to our requirements.

Instance methods

instance method

When we define a method inside a class by default it becomes a instance method means we can have access to that particular method with the instances of class or we can say  objects of class.
 

Example

class Message
    def display_message
        puts 'welcome to coding ninjas'
    end 
end 

obj1 = Message.new 
obj2 = Message.new

obj1.display_message
obj2.display_message
You can also try this code with Online Ruby Compiler
Run Code

 

Output

output1

In the above example, we can see that we have access to the display_message method in class Message with two different objects or instances that's how the default methods or we can say instance methods can be accessed.

Now let us talk about Singleton Methods: 

Singleton methods

singleton method

Singleton methods or class methods are those methods that are defined using only for a single object, not by the class of objects.
 

Syntax:

def Classname.method_name
        #method functionality
end
You can also try this code with Online Ruby Compiler
Run Code


Example

class Alert 
    def Alert.notification
        puts 'here is a notification'
    end 
    def Alert.message
        puts "coding ninjas"
    end 
end 

Alert.notification
Alert.message
puts Alert.instance_methods.include?(:notification) 
puts Alert.singleton_methods.include?(:notification) 
You can also try this code with Online Ruby Compiler
Run Code


Output:

output2

 

In the above example, we can see that we have not created any instance or object to call both of the methods instead we are using classname to make the methods into singleton methods or class methods. we can confirm this with 

puts Alert.instance_methods.include?(:notification) ->this returns false

puts Alert.singleton_methods.include?(:notification) -> this returns true

We can also use the self keyword instead of the class name. 

Let's see the above example with the self keyword.
 

Example

class Alert 
    def self.notification
        puts 'here is a notification'
    end 
    def self.message
        puts "coding ninjas"
    end 
end 


Alert.notification
Alert.message
You can also try this code with Online Ruby Compiler
Run Code


Output:

output3

As we know ruby is a pure object-oriented language meaning almost everything in ruby is an object. So these singleton methods or class methods behind the scene are also instance methods of an anonymous class known as eigenclass.

Eigenclass

eigenclass image

An unnamed instance of the class Class_name that is connected to an object and whose instance methods are utilised as singleton methods of the defined object is referred to as an eigenclass.

If you want to explicitly open an eigenclass, Self can be used in place of the class name when opening an object's eigenclass within the definition of the class itself.
 

Syntax

class Class_name
    #instance methods here
    class << self
        #class methods here as instance method of eigenclass
    end 
end 
You can also try this code with Online Ruby Compiler
Run Code


The above syntax shows the working behind the scenes of the eigenclass generally it is not ideal to explicitly open the eigenclass instead it is much better to define class methods as individual singleton methods.

Self is a reference to the eigenclass object when you open the eigenclass of an object. Therefore, the idiom for discovering an object's eigenclass is:
 

Eigenclass = class << object; self; end
You can also try this code with Online Ruby Compiler
Run Code


This may be formalised as a method of Object, allowing us to request the eigenclass of any object:

 

Example

class Object
    def self.eigenclass
        class << self
            self
        end
    end 
end 


puts Object.eigenclass
You can also try this code with Online Ruby Compiler
Run Code


Output:

output4

Frequently asked questions

Are there class methods in Ruby?

A class method is a singleton method of a class object. (In actuality, the metaclass contains a definition for the class method, but that information is apparent.) A different perspective would be to define a class method as a method whose recipient is a class.

It all comes down to the fact that class methods can be called without requiring that the receivers be actual instances of the class (objects).

Define singleton methods?

Subclassing the class linked to a specific object results in the creation of an anonymous class known as a singleton class. Singleton classes are an additional method for extending the capabilities linked to a single object.

What is the self keyword?

The object to which a method is applied is called the receiver, and it is now being executed. Self is implied by a function-style method call as the receiver

How many ways we can define class methods in ruby?

In Ruby, there are two standard approaches for defining class methods. The first is the "def self. method" (also known as Style #1), and the second is the "class << self" (also known as Style #2).

What differentiates a class from a module?

Methods and constants are gathered in modules. They are unable to produce instances. Classes have a per-instance state and can produce instances (objects) (instance variables). Classes and other modules may contain a mix of modules. Constants and methods from the merged module are incorporated into the class's own, enhancing its functionality. However, classes cannot be combined with anything. A class can't inherit from a module, although it may from another class. You cannot inherit from a module.

Conclusion

In this article, we learned about the singleton methods and eigenclass in ruby and how we can implement them in our code. Also we have learned the relationship between the singleton methods and eigenclass.

To learn about more ruby please look into the following articles:

 8 reasons why ruby should be your first language

Ruby and ruby on rails: differences between them

Ruby on rails

To learn more about DSA, competitive coding and many more knowledgeable topics, please look into the guided paths on Coding Ninjas Studio. Also, you can enroll in our courses and check out the mock test and problems available to you. Please check out our interview experiences and interview bundle for placement preparations.

thank you image

Please upvote our blog to help other ninjas grow.

Happy Learning

Live masterclass