# Singleton classes and Singleton pattern

**URL:** <https://www.ruby-forum.com/t/singleton-classes-and-singleton-pattern/71796>\
**Category:** Ruby\
**Created:** [October 11, 2006, 3:05pm UTC](https://www.ruby-forum.com/t/singleton-classes-and-singleton-pattern/71796 "2006-10-11T15:05:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wilhelm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ce7236/32.png) [@wilhelm](https://www.ruby-forum.com/u/wilhelm)\
**Post date:** [October 11, 2006, 3:05pm UTC](https://www.ruby-forum.com/t/singleton-classes-and-singleton-pattern/71796/1 "2006-10-11T15:05:35Z")

</div>

I just happened upon singleton classes, and was wondering two things:

1. There’s no difference between these two codes, right? Is there an  
advantage or disadvantage over either? Does Ruby put bar() into the  
same virtual class object for both?

class Foo  
class \<\< self  
def bar  
end  
end  
end

class Foo  
def self.bar  
end  
end

1. A singleton class has nothing to do with the singleton design  
pattern, right? They only share the same name because there is only  
ever one of them.

---

<div class="post-metadata">

**Author:** ![wilhelm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ce7236/32.png) [@wilhelm](https://www.ruby-forum.com/u/wilhelm)\
**Post date:** [October 11, 2006, 3:09pm UTC](https://www.ruby-forum.com/t/singleton-classes-and-singleton-pattern/71796/2 "2006-10-11T15:09:38Z")

</div>

Hi –

On Wed, 11 Oct 2006, Wilhelm wrote:

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

See the thread from a couple of days ago called ‘“def self.method” vs  
“class \<\< self; def method”’.

> 1. A singleton class has nothing to do with the singleton design  
> pattern, right? They only share the same name because there is only  
> ever one of them.

That’s correct. The predicate “singleton” applies to both, but  
they’re not the same as each other.

David
