# Singleton definition differences

**URL:** <https://www.ruby-forum.com/t/singleton-definition-differences/126886>\
**Category:** Ruby\
**Created:** [January 23, 2008, 4:10am UTC](https://www.ruby-forum.com/t/singleton-definition-differences/126886 "2008-01-23T04:10:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickrahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dfb087/32.png) [@nickrahn](https://www.ruby-forum.com/u/nickrahn)\
**Post date:** [January 23, 2008, 4:10am UTC](https://www.ruby-forum.com/t/singleton-definition-differences/126886/1 "2008-01-23T04:10:45Z")

</div>

would someone please explain the difference between these definitions

class A  
class \<\< self  
def hello  
puts ‘hello’  
end  
end  
end

class B  
def B.hello  
puts ‘hello’  
end  
end

A.hello  
B.hello

---

<div class="post-metadata">

**Author:** ![nickrahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dfb087/32.png) [@nickrahn](https://www.ruby-forum.com/u/nickrahn)\
**Post date:** [January 23, 2008, 9:13am UTC](https://www.ruby-forum.com/t/singleton-definition-differences/126886/2 "2008-01-23T09:13:18Z")

</div>

Nicholas R. wrote:

> would someone please explain the difference between these definitions
> 
> class A  
> class \<\< self  
> def hello  
> puts ‘hello’  
> end  
> end  
> end
> 
> class B  
> def B.hello  
> puts ‘hello’  
> end  
> end
> 
> A.hello  
> B.hello

According to p. 36, “Programming Ruby(2nd ed.)”, they are different  
syntaxes for accomplishing the same thing. If you define class A like  
this:

class A  
def sayhi  
puts “hi”  
end  
end

a = A.new  
a.sayhi

then you have this situation:

a – instance of class A  
|  
|  
|  
V  
class A  
methods:  
—sayhi()  
|  
|  
|  
V  
class Object  
methods:  
—send(), etc.

If you define class A like you did:

> class A  
> class \<\< self  
> def hello  
> puts ‘hello’  
> end  
> end  
> end

then this is what happens:

a – instance of class A  
|  
|  
|  
V  
class A  
methods:  
–   
|  
|  
|  
V  
class A’ – a superclass ( or “singleton class”) for A only containing  
A’s class methods  
methods:  
—hello()  
|  
|  
|  
V  
class Object  
methods  
–send(), etc.

In effect, you inserted a new class between class A and class Object.  
The dot notation does the same thing.

---

<div class="post-metadata">

**Author:** ![nickrahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dfb087/32.png) [@nickrahn](https://www.ruby-forum.com/u/nickrahn)\
**Post date:** [January 23, 2008, 9:18am UTC](https://www.ruby-forum.com/t/singleton-definition-differences/126886/3 "2008-01-23T09:18:15Z")

</div>

7stud wrote:

> In effect, you inserted a new class between class A and class Object.  
> The dot notation does the same thing.

When you call a method with object a, ruby searches class A for the  
method. If ruby can’t find the method in class A, ruby moves up the  
hierarchy and searches class A’ (which contains the class methods) for  
the method. If the method can’t be found in class A’, then ruby  
searches Object for the method.

---

<div class="post-metadata">

**Author:** ![nickrahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dfb087/32.png) [@nickrahn](https://www.ruby-forum.com/u/nickrahn)\
**Post date:** [January 23, 2008, 9:22am UTC](https://www.ruby-forum.com/t/singleton-definition-differences/126886/4 "2008-01-23T09:22:11Z")

</div>

7stud – wrote:

> If the method can’t be found in class A’, \<**then ruby  
> searches Object for the method.**\>

Whoops. That’s not true, but I won’t elaborate.

---

<div class="post-metadata">

**Author:** ![nickrahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dfb087/32.png) [@nickrahn](https://www.ruby-forum.com/u/nickrahn)\
**Post date:** [January 23, 2008, 11:30am UTC](https://www.ruby-forum.com/t/singleton-definition-differences/126886/5 "2008-01-23T11:30:59Z")

</div>

Not to hijack the thread but does singleton not refer to a pattern  
method that instantiates and returns an object reference and for future  
calls returns the reference of the object which has been cached in  
memory to prevent instantiation of duplicate instances

---

<div class="post-metadata">

**Author:** ![nickrahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dfb087/32.png) [@nickrahn](https://www.ruby-forum.com/u/nickrahn)\
**Post date:** [January 23, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/singleton-definition-differences/126886/6 "2008-01-23T12:02:56Z")

</div>

On Jan 23, 2008 11:30 AM, Keynan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Not to hijack the thread but does singleton not refer to a pattern  
> method that instantiates and returns an object reference and for future  
> calls returns the reference of the object which has been cached in  
> memory to prevent instantiation of duplicate instances

There are singleton methods in Ruby. They have nothing in common with  
the Singleton pattern as you described it. You can define singleton  
methods for objects, e.g. in irb  
irb(main):001:0\> foo = “Hello World”  
=\> “Hello World”  
irb(main):002:0\> class \<\< foo  
irb(main):003:1\> def shuffle  
irb(main):004:2\> split(//).sort { rand }.join ‘’  
irb(main):005:2\> end  
irb(main):006:1\> end  
=\> nil  
irb(main):007:0\> foo.shuffle  
=\> “d HWloorlle”

As you can see, you can modify the methods of single objects instead  
modifying the whole class.

---

<div class="post-metadata">

**Author:** ![nickrahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dfb087/32.png) [@nickrahn](https://www.ruby-forum.com/u/nickrahn)\
**Post date:** [January 23, 2008, 12:07pm UTC](https://www.ruby-forum.com/t/singleton-definition-differences/126886/7 "2008-01-23T12:07:39Z")

</div>

On Jan 23, 2008 4:10 AM, Nicholas R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class B  
> def B.hello  
> puts ‘hello’  
> end  
> end
> 
> A.hello  
> B.hello

I _think_, there’s no difference between them and I think also, that  
they are not called Singleton methods, but class methods.

---

<div class="post-metadata">

**Author:** ![nickrahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dfb087/32.png) [@nickrahn](https://www.ruby-forum.com/u/nickrahn)\
**Post date:** [January 23, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/singleton-definition-differences/126886/8 "2008-01-23T15:05:28Z")

</div>

On Jan 23, 2008, at 6:05 AM, Thomas W. wrote:

> they are not called Singleton methods, but class methods.

However, they are methods that belong to a single instance of a class  
object, thus making them singleton. So, in fact, they are both.
