Singleton method
Definition
A singleton method
, in the context of object-oriented programming, is a method that is defined for a single instance (or object) of a class rather than for the class as a whole.
It's called "singleton" because it belongs to a single instance of the class, rather than being shared among all instances.
A
singleton method
is a method that is defined for a single object rather than a class. This means that the method is only available to that particular object and not to other instances of the same class.
Ruby
As we said, a singleton method
belongs to a specific instance of a class, rather than being shared among all instances.
In order to implement a singleton method
in Ruby, it's necessary to use the object's name, followed by .
(dot) and choose a method name.
It'll look something like obj.method_name
.
Implementation
obj = Object.new
def obj.my_singleton_method(value)
puts "#{value} comes from the singleton method in #{self}"
end
obj.my_singleton_method('Hello')
In the previous example, the my_singleton_method
is defined only for the obj
instance, and it won't be available for other instances of the same class.
Unavailable for other instances
obj = Object.new
obj2 = Object.new
def obj.my_singleton_method(value)
puts "#{value} comes from the singleton method in #{self}"
end
obj.my_singleton_method('Hello')
obj2.my_singleton_method('Hello')
In case you try to use my_singleton_method
on a another object, you'll get an undefined method
.
Done
Conclusion
By following these steps, you can easily visualize how singleton methods work in Ruby.
Celebrate
Reach me out
Final thoughts
Thank you for reading this article.
If you have any questions, thoughts, suggestions, or corrections, please share them with us.
We appreciate your feedback and look forward to hearing from you.
Feel free to suggest topics for future blog articles. Until next time!