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 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
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
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 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
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
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
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
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
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
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: