Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are singleton methods useful? [closed]

Tags:

ruby

singleton

I am pretty new to Ruby and have learned it syntax along with singleton methods. I understand how to create and use them. But why use them?

Can someone give me a few real world cases where using singleton methods are helpful?

Thank You

like image 899
user1763684 Avatar asked Jul 14 '26 05:07

user1763684


2 Answers

Maybe you want to do some debugging? Say you have a collection of objects, and you know one of them acts up while being iterated over alongside it's siblings... so you modify a problematic method to wrap it in some logging / tracing code that saves you having to read pages of logging statements for the 100 other items.

That's at least one contrived example I can think of.

like image 118
Koobz Avatar answered Jul 20 '26 13:07

Koobz


  • http://www.rubyist.net/~slagell/ruby/singletonmethods.html
  • http://www.ruby-forum.com/topic/86169 ;
  • http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html

Singleton methods: It is often useful to be able to define special behavior for an individual object.

like image 21
yeyo Avatar answered Jul 20 '26 11:07

yeyo