# Singleton object vs. enhancing singleton class

**URL:** <https://www.ruby-forum.com/t/singleton-object-vs-enhancing-singleton-class/139921>\
**Category:** Ruby\
**Created:** [June 9, 2008, 5:37am UTC](https://www.ruby-forum.com/t/singleton-object-vs-enhancing-singleton-class/139921 "2008-06-09T05:37:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_McMahon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@Paul\_McMahon](https://www.ruby-forum.com/u/Paul_McMahon)\
**Post date:** [June 9, 2008, 5:37am UTC](https://www.ruby-forum.com/t/singleton-object-vs-enhancing-singleton-class/139921/1 "2008-06-09T05:37:20Z")

</div>

In ruby, as class are singleton objects, does anyone have any ideas when  
a a singleton object should be used vs. simply adding methods to the  
class? For example,

class Foo  
include Singleton  
def bar  
end  
end

vs.

class Foo  
def self.bar  
end  
end

---

<div class="post-metadata">

**Author:** ![Paul\_McMahon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@Paul\_McMahon](https://www.ruby-forum.com/u/Paul_McMahon)\
**Post date:** [June 9, 2008, 6:04am UTC](https://www.ruby-forum.com/t/singleton-object-vs-enhancing-singleton-class/139921/2 "2008-06-09T06:04:53Z")

</div>

On Sunday 08 June 2008 22:35:25 Paul McMahon wrote:

> In ruby, as class are singleton objects, does anyone have any ideas when  
> a a singleton object should be used vs. simply adding methods to the  
> class?

I always include Singleton, but I see what you mean. A better way might  
be to  
do it with modules:

module Foo  
def self.bar  
end  
end

One example is mixins, though. For example, here:

> class Foo  
> def self.bar  
> end  
> end

A mixin would almost certainly be expecting to be mixed via “include”  
here,  
not “extend”. This might not always matter, but some mixins will define  
things like self.included, and won’t work at all if you try to extend  
them  
instead.

However, a mixin which expected “extend” would still work on a Singleton  
object:

class Foo  
include Singleton  
def bar  
end  
end  
Foo.instance.extend SomeMixin

I think the main reason for doing Singleton, though, is that it  
expresses  
intent better, and it prevents the class from being instantiated –  
sure,  
there are ways around it, but you’re not going to accidentally call  
Foo.new  
without getting an error.

---

<div class="post-metadata">

**Author:** ![Paul\_McMahon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@Paul\_McMahon](https://www.ruby-forum.com/u/Paul_McMahon)\
**Post date:** [June 9, 2008, 6:11am UTC](https://www.ruby-forum.com/t/singleton-object-vs-enhancing-singleton-class/139921/3 "2008-06-09T06:11:20Z")

</div>

In reply to myself:

> it prevents the class from being instantiated

To clarify – prevents it from being instantiated again.

---

<div class="post-metadata">

**Author:** ![Paul\_McMahon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@Paul\_McMahon](https://www.ruby-forum.com/u/Paul_McMahon)\
**Post date:** [June 9, 2008, 8:07am UTC](https://www.ruby-forum.com/t/singleton-object-vs-enhancing-singleton-class/139921/4 "2008-06-09T08:07:28Z")

</div>

Hi –

On Mon, 9 Jun 2008, Paul McMahon wrote:

> In ruby, as class are singleton objects, does anyone have any ideas when

I’d just say: classes are objects. Like other objects, they can have  
methods added to them on a per-object basis (give or take the fact  
that “per-object” in the case of a class actually includes  
subclasses).

> class Foo  
> def self.bar  
> end  
> end

I don’t see these techniques as addressing the same problem or being  
likely candidates to replace each other. I’d go by what you need. If  
you need a class that has only one instance, then use Singleton. If  
you’ve got a situation where there’s class-level (as opposed to  
instance-level) knowledge involved, then use a class method.

David
