# Singleton Method

**URL:** <https://rubytalk.org/t/singleton-method/43207>\
**Category:** ruby-talk\
**Created:** [21 December 2007 23:51 UTC](https://rubytalk.org/t/singleton-method/43207 "2007-12-21T23:51:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Lewis](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@Ryan\_Lewis](https://rubytalk.org/u/Ryan_Lewis)\
**Post date:** [21 December 2007 23:51 UTC](https://rubytalk.org/t/singleton-method/43207/1 "2007-12-21T23:51:25Z")

</div>

What's the difference between a regular method and a singleton method?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [22 December 2007 00:29 UTC](https://rubytalk.org/t/singleton-method/43207/2 "2007-12-22T00:29:15Z")

</div>

> What's the difference between a regular method and a singleton method?

From the perspective of a particular object, singleton methods are  
those methods that are defined within the singleton class of  
the object.

Regular methods are methods that are defined within the normal  
inheritance tree of the object's class.

Method lookup is done by first checking the singleton class of an  
object and if not found there by checking the normal inheritance tree.

By default objects don't have a singleton class. The singleton class  
is created when it is referenced:

$ irb  
\>\> a = [1,2,3]  
=\> [1, 2, 3]  
\>\> a.second  
NoMethodError: undefined method `second' for [1, 2, 3]:Array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):2  
\>\> class \<\<a  
\>\> def second  
\>\> self[1]  
\>\> end  
\>\> end  
=\> nil  
\>\> a.second  
=\> 2  
\>\> [4, 5, 6].second  
NoMethodError: undefined method `second' for [4, 5, 6]:Array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):9  
\>\>

In this example, I opened up the singleton class associated  
with 'a' and defined a new method, second, that returns the  
second element of the array. As you can see, this method  
is only available to that one particular array and not to  
all array's in general.

Gary Wright

> **···**
>
> On Dec 21, 2007, at 6:51 PM, Ryan Lewis wrote:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0

---

<div class="post-metadata">

**Author:** ![Joon\_You1](https://avatars.discourse-cdn.com/v4/letter/j/eada6e/32.png) [@Joon\_You1](https://rubytalk.org/u/Joon_You1)\
**Post date:** [22 December 2007 01:24 UTC](https://rubytalk.org/t/singleton-method/43207/3 "2007-12-22T01:24:08Z")

</div>

Just out of curiosity, is there a difference between

def a.second  
...  
end

and

class \<\< a

?

Gary Wright wrote:

> **···**
>
> > On Dec 21, 2007, at 6:51 PM, Ryan Lewis wrote:
> > 
> > > What's the difference between a regular method and a singleton method?
> > 
> > From the perspective of a particular object, singleton methods are  
> > those methods that are defined within the singleton class of  
> > the object.
> > 
> > Regular methods are methods that are defined within the normal  
> > inheritance tree of the object's class.
> > 
> > Method lookup is done by first checking the singleton class of an  
> > object and if not found there by checking the normal inheritance tree.
> > 
> > By default objects don't have a singleton class. The singleton class  
> > is created when it is referenced:
> > 
> > $ irb  
> > \>\> a = [1,2,3]  
> > =\> [1, 2, 3]  
> > \>\> a.second  
> > NoMethodError: undefined method `second' for [1, 2, 3]:Array  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):2  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0  
> > \>\> class \<\<a  
> > \>\> def second  
> > \>\> self[1]  
> > \>\> end  
> > \>\> end  
> > =\> nil  
> > \>\> a.second  
> > =\> 2  
> > \>\> [4, 5, 6].second  
> > NoMethodError: undefined method `second' for [4, 5, 6]:Array  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):9  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0  
> > \>\>
> > 
> > In this example, I opened up the singleton class associated  
> > with 'a' and defined a new method, second, that returns the  
> > second element of the array. As you can see, this method  
> > is only available to that one particular array and not to  
> > all array's in general.
> > 
> > Gary Wright
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ryan\_Lewis](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@Ryan\_Lewis](https://rubytalk.org/u/Ryan_Lewis)\
**Post date:** [22 December 2007 06:35 UTC](https://rubytalk.org/t/singleton-method/43207/4 "2007-12-22T06:35:11Z")

</div>

So defining the 'second' method like so:  
&nbsp;&nbsp;&nbsp;def Array.second  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self[1]  
&nbsp;&nbsp;&nbsp;end

is the same is making the singleton class like below.  
&nbsp;&nbsp;&nbsp;Class \<\< Array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def second  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self[1]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

Gotcha.

Thanks for the help! =)

Gary Wright wrote:

> **···**
>
> > On Dec 21, 2007, at 6:51 PM, Ryan Lewis wrote:  
> > In this example, I opened up the singleton class associated  
> > with 'a' and defined a new method, second, that returns the  
> > second element of the array. As you can see, this method  
> > is only available to that one particular array and not to  
> > all array's in general.
> > 
> > Gary Wright
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ben\_Lipton](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@Ben\_Lipton](https://rubytalk.org/u/Ben_Lipton)\
**Post date:** [22 December 2007 02:59 UTC](https://rubytalk.org/t/singleton-method/43207/5 "2007-12-22T02:59:35Z")

</div>

I think the answer is No, there isn't a difference, but it's something I've  
been wondering about.  
From [Jay Fields' Thoughts: Ruby: Class Methods](http://blog.jayfields.com/2007/04/ruby-class-methods.html) we have:

"In fact, you can define a class method various ways, but the result is  
always an instance method on the singleton class."

class Parser  
&nbsp;&nbsp;def self.self\_process(script)  
&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;end

&nbsp;&nbsp;def Parser.parser\_process(script)  
&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;end

&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;def singleton\_process(script)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;def self.singleton  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self; self; end  
&nbsp;&nbsp;end  
end

Now, this is talking about class methods, not singleton methods on arbitrary  
objects, but I think the idea still applies because class methods are  
methods on a singleton, in this case the object Parser (which happens to be  
a Class). So, the second and the third examples are the ones you're asking  
about, because inside the "class Parser ... end", self is equal to Parser.  
So, he's doing

def Parser.parser\_process(script)  
end

and

class \<\< Parser  
&nbsp;&nbsp;def singleton\_process(script)  
&nbsp;&nbsp;end  
end

just like your example.

> **···**
>
> On Dec 21, 2007 8:24 PM, Joon You \<joon@rubyhead.com\> wrote:
> 
> > Just out of curiosity, is there a difference between
> > 
> > def a.second  
> > ...  
> > end
> > 
> > and
> > 
> > class \<\< a
> > 
> > ?
> > 
> > Gary Wright wrote:  
> > \> On Dec 21, 2007, at 6:51 PM, Ryan Lewis wrote:  
> > \>  
> > \>\> What's the difference between a regular method and a singleton method?  
> > \>  
> > \> From the perspective of a particular object, singleton methods are  
> > \> those methods that are defined within the singleton class of  
> > \> the object.  
> > \>  
> > \> Regular methods are methods that are defined within the normal  
> > \> inheritance tree of the object's class.  
> > \>  
> > \> Method lookup is done by first checking the singleton class of an  
> > \> object and if not found there by checking the normal inheritance tree.  
> > \>  
> > \> By default objects don't have a singleton class. The singleton class  
> > \> is created when it is referenced:  
> > \>  
> > \> $ irb  
> > \> \>\> a = [1,2,3]  
> > \> =\> [1, 2, 3]  
> > \> \>\> a.second  
> > \> NoMethodError: undefined method `second' for [1, 2, 3]:Array  
> > \> from (irb):2  
> > \> from :0  
> > \> \>\> class \<\<a  
> > \> \>\> def second  
> > \> \>\> self[1]  
> > \> \>\> end  
> > \> \>\> end  
> > \> =\> nil  
> > \> \>\> a.second  
> > \> =\> 2  
> > \> \>\> [4, 5, 6].second  
> > \> NoMethodError: undefined method `second' for [4, 5, 6]:Array  
> > \> from (irb):9  
> > \> from :0  
> > \> \>\>  
> > \>  
> > \> In this example, I opened up the singleton class associated  
> > \> with 'a' and defined a new method, second, that returns the  
> > \> second element of the array. As you can see, this method  
> > \> is only available to that one particular array and not to  
> > \> all array's in general.  
> > \>  
> > \> Gary Wright
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [22 December 2007 07:51 UTC](https://rubytalk.org/t/singleton-method/43207/6 "2007-12-22T07:51:14Z")

</div>

No. Your examples are different than mine.  
In your examples you are defining a singleton method  
for 'Array', which is an object that just happens  
to be a class.

In my examples I defined a singleton method for one  
particular array, not all arrays, and not the object  
known as Array.

Let's see if I can summarize things a bit:

All method definitions wind up being stored in a  
module of one sort or another (which includes  
classes since Class is a subclass of Module,  
i.e. every class is also a module).

1) standard modules (instances of Module)  
&nbsp;&nbsp;&nbsp;&nbsp;-- the methods are called instance methods

2) standard classes (instances of Class)  
&nbsp;&nbsp;&nbsp;&nbsp;-- the methods are called instance methods

3) singleton classes of non-class/non-module objects  
&nbsp;&nbsp;&nbsp;&nbsp;-- the methods are called singleton methods

4) singleton classes of instances of Module  
&nbsp;&nbsp;&nbsp;-- the methods are called module methods or  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton methods of a (particular) module

5) singleton classes of instances of Class  
&nbsp;&nbsp;&nbsp;-- the methods are called class methods or  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton methods of a (particular) class.

Since singleton classes are also classes, you can  
get an infinite regress of singleton methods of  
a singleton class of a singleton class of a...  
But just set that aside for the moment. Here is  
what the code looks like for the cases I listed:

# case 1  
module Enumerable  
&nbsp;&nbsp;&nbsp;def foo; end  
end

# case 2  
class Array  
&nbsp;&nbsp;&nbsp;def foo; end  
end

# case 3  
a = Object.new  
def a.foo; end

# also case 3  
class \<\<a  
&nbsp;&nbsp;&nbsp;def foo; end  
end

# case 4  
def Enumerable.foo  
end

# also case 4  
module Enumerable  
&nbsp;&nbsp;&nbsp;def self.foo  
&nbsp;&nbsp;&nbsp;end  
end

# also case 4  
module Enumerable  
&nbsp;&nbsp;&nbsp;class \<\<self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def foo; end  
&nbsp;&nbsp;&nbsp;end  
end

# still case 4  
class \<\<Enumerable  
&nbsp;&nbsp;&nbsp;def foo; end  
end

# case 5  
def Array.foo  
end

# also case 5  
class Array  
&nbsp;&nbsp;&nbsp;def self.foo  
&nbsp;&nbsp;&nbsp;end  
end

# also case 5  
class Array  
&nbsp;&nbsp;&nbsp;class \<\<self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def foo; end  
&nbsp;&nbsp;&nbsp;end  
end

# still case 5  
class \<\<Array  
&nbsp;&nbsp;&nbsp;def foo; end  
end

I hope that helps.

Gary Wright

> **···**
>
> On Dec 22, 2007, at 1:35 AM, Ryan Lewis wrote:
> 
> > So defining the 'second' method like so:  
> > &nbsp;&nbsp;&nbsp;def Array.second  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self[1]  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > is the same is making the singleton class like below.  
> > &nbsp;&nbsp;&nbsp;Class \<\< Array  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def second  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self[1]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [22 December 2007 05:20 UTC](https://rubytalk.org/t/singleton-method/43207/7 "2007-12-22T05:20:46Z")

</div>

I think your description is a bit misleading.

A class method is a singleton method of a class object.  
A class is an instance of Class.

What I'm trying to point out is that class objects  
are not 'singletons' in the sense of the one-and-only  
instance of a particular class, which it appears to be  
what you were alluding to if I'm not mistaken.  
There are many instances of Class: Array, Object,  
Parser, Hash, and so on.

This is yet another example where the word 'singleton',  
as in singleton method and singleton class, can get confused  
with the more generic use of singleton (which I don't  
think applies here in any case).

Gary Wright

> **···**
>
> On Dec 21, 2007, at 9:59 PM, Ben Lipton wrote:
> 
> > Now, this is talking about class methods, not singleton methods on arbitrary  
> > objects, but I think the idea still applies because class methods are  
> > methods on a singleton, in this case the object Parser (which happens to be  
> > a Class).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [22 December 2007 07:59 UTC](https://rubytalk.org/t/singleton-method/43207/8 "2007-12-22T07:59:58Z")

</div>

Gary Wright wrote:

> \> So defining the 'second' method like so:  
> \> def Array.second  
> \> self[1]  
> \> end  
> \>  
> \> is the same is making the singleton class like below.  
> \> Class \<\< Array  
> \> def second  
> \> self[1]  
> \> end  
> \> end
> 
> No.

Yes, actually.

> Your examples are different than mine.

They are. They also don't make much sense. But they're still equivalent  
to each other.

> # case 5  
> def Array.foo  
> end  
> [...]  
> # still case 5  
> class \<\<Array  
> &nbsp;&nbsp;&nbsp;def foo; end  
> end

See? Both case 5.

> **···**
>
> > On Dec 22, 2007, at 1:35 AM, Ryan Lewis wrote:
> 
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Pat\_Maddox1](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@Pat\_Maddox1](https://rubytalk.org/u/Pat_Maddox1)\
**Post date:** [22 December 2007 08:24 UTC](https://rubytalk.org/t/singleton-method/43207/9 "2007-12-22T08:24:13Z")

</div>

His question is, "are these the same?" to which the answer is simply yes.

Pat

> **···**
>
> On Dec 21, 2007 11:51 PM, Gary Wright \<gwtmp01@mac.com\> wrote:
> 
> > On Dec 22, 2007, at 1:35 AM, Ryan Lewis wrote:
> > 
> > \> So defining the 'second' method like so:  
> > \> def Array.second  
> > \> self[1]  
> > \> end  
> > \>  
> > \> is the same is making the singleton class like below.  
> > \> Class \<\< Array  
> > \> def second  
> > \> self[1]  
> > \> end  
> > \> end
> > 
> > No.  
> > \<snip tons of stuff\>

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [22 December 2007 13:59 UTC](https://rubytalk.org/t/singleton-method/43207/10 "2007-12-22T13:59:25Z")

</div>

I was trying to point out that they didn't make sense relative  
to the previous examples in the thread, but Sebastian is correct  
to point out that they are equivalent to each other.

It turns out that those definitions for Array.second end  
up returning arrays, in particular they return [1], since Array  
actually has a method, which construct and returns a new array.  
In Ryan's code, self[1], is actually a call to Array., which  
constructs the new array.

Array.second returns [1] in those examples, something very different  
what I was showing in my examples.

Gary.

> **···**
>
> On Dec 22, 2007, at 2:59 AM, Sebastian Hungerecker wrote:
> 
> > Gary Wright wrote:
> > 
> > > On Dec 22, 2007, at 1:35 AM, Ryan Lewis wrote:
> > > 
> > > > So defining the 'second' method like so:  
> > > > &nbsp;&nbsp;&nbsp;def Array.second  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self[1]  
> > > > &nbsp;&nbsp;&nbsp;end
> > > > 
> > > > is the same is making the singleton class like below.  
> > > > &nbsp;&nbsp;&nbsp;Class \<\< Array  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def second  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self[1]  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > > &nbsp;&nbsp;&nbsp;end
> > > 
> > > No.
> > 
> > Yes, actually.
> > 
> > > Your examples are different than mine.
> > 
> > They are. They also don't make much sense. But they're still equivalent  
> > to each other.

---

<div class="post-metadata">

**Author:** ![Ben\_Lipton](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@Ben\_Lipton](https://rubytalk.org/u/Ben_Lipton)\
**Post date:** [22 December 2007 14:06 UTC](https://rubytalk.org/t/singleton-method/43207/11 "2007-12-22T14:06:17Z")

</div>

You're right, my description is misleading, though I think I had it  
correctly in my head. I'm new to Ruby and still figuring out my terminology.  
Thanks for clearing that up.

> **···**
>
> On Dec 22, 2007 12:20 AM, Gary Wright \<gwtmp01@mac.com\> wrote:
> 
> > On Dec 21, 2007, at 9:59 PM, Ben Lipton wrote:  
> > \> Now, this is talking about class methods, not singleton methods on  
> > \> arbitrary  
> > \> objects, but I think the idea still applies because class methods are  
> > \> methods on a singleton, in this case the object Parser (which  
> > \> happens to be  
> > \> a Class).
> > 
> > I think your description is a bit misleading.
> > 
> > A class method is a singleton method of a class object.  
> > A class is an instance of Class.
> > 
> > What I'm trying to point out is that class objects  
> > are not 'singletons' in the sense of the one-and-only  
> > instance of a particular class, which it appears to be  
> > what you were alluding to if I'm not mistaken.  
> > There are many instances of Class: Array, Object,  
> > Parser, Hash, and so on.
> > 
> > This is yet another example where the word 'singleton',  
> > as in singleton method and singleton class, can get confused  
> > with the more generic use of singleton (which I don't  
> > think applies here in any case).
> > 
> > Gary Wright
