# Class/singleton methods

**URL:** <https://rubytalk.org/t/class-singleton-methods/21276>\
**Category:** ruby-talk\
**Created:** [3 October 2005 02:30 UTC](https://rubytalk.org/t/class-singleton-methods/21276 "2005-10-03T02:30:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Volkmann1](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@Mark\_Volkmann1](https://rubytalk.org/u/Mark_Volkmann1)\
**Post date:** [3 October 2005 02:30 UTC](https://rubytalk.org/t/class-singleton-methods/21276/1 "2005-10-03T02:30:58Z")

</div>

These seem to be identical. Are they?

class Foo  
&nbsp;&nbsp;def Foo.bar  
&nbsp;&nbsp;&nbsp;&nbsp;# some code  
&nbsp;&nbsp;end  
end

class Foo  
&nbsp;&nbsp;def self.bar  
&nbsp;&nbsp;&nbsp;&nbsp;# some code  
&nbsp;&nbsp;end  
end

If they are identical, is one form generally preferred over the other?

So a "class method" is the same as a "singleton method" on a Class object?

> **···**
>
> --  
> R. Mark Volkmann  
> Partner, Object Computing, Inc.

---

<div class="post-metadata">

**Author:** ![Rob\_Rypka](https://avatars.discourse-cdn.com/v4/letter/r/e47c2d/32.png) [@Rob\_Rypka](https://rubytalk.org/u/Rob_Rypka)\
**Post date:** [3 October 2005 03:03 UTC](https://rubytalk.org/t/class-singleton-methods/21276/2 "2005-10-03T03:03:52Z")

</div>

> These seem to be identical. Are they?
> 
> class Foo  
> &nbsp;&nbsp;def Foo.bar  
> &nbsp;&nbsp;&nbsp;&nbsp;# some code  
> &nbsp;&nbsp;end  
> end
> 
> class Foo  
> &nbsp;&nbsp;def self.bar  
> &nbsp;&nbsp;&nbsp;&nbsp;# some code  
> &nbsp;&nbsp;end  
> end

They are identical. Inside Foo, self==Foo

> If they are identical, is one form generally preferred over the other?

I think it's more a matter of style. The advantage of using 'self' is  
that it always works with the class name (so you won't mess up cutting  
and pasting or changing the name of the class), but it's less clear  
what it does to others (you had to ask).

> So a "class method" is the same as a "singleton method" on a Class object?

Umm, yes? I'm not always sure what those terms refer to.

I think reading this will help sort out your issues:

> **[WebMD - Better information. Better health.](https://www.webmd.com/?ClassMethods)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

> **···**
>
> On 10/2/05, Mark Volkmann \<r.mark.volkmann@gmail.com\> wrote:
> 
> --  
> Rob

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [3 October 2005 03:07 UTC](https://rubytalk.org/t/class-singleton-methods/21276/3 "2005-10-03T03:07:25Z")

</div>

Hi --

> These seem to be identical. Are they?
> 
> class Foo  
> def Foo.bar  
> &nbsp;&nbsp;&nbsp;# some code  
> end  
> end
> 
> class Foo  
> def self.bar  
> &nbsp;&nbsp;&nbsp;# some code  
> end  
> end
> 
> If they are identical, is one form generally preferred over the other?

The advantage of the self version is that it might be a little easier  
to maintain (e.g., if you change the name of the class).

> So a "class method" is the same as a "singleton method" on a Class object?

Basically, yes. I think the main difference is that they have a bit  
of language-level "special case" status. There's the class\_methods  
method, which makes them "official". Also, because of the way  
classes' singleton classes work, a subclass can actually call the  
singleton methods of its parent class:

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.x  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Calling x on #{self}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class D \< C  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;D.x # Calling x on D

As far as I know that's the only situation where an object can call  
another object's singleton method. That may be enough to warrant a  
different term for them. I have to say, though, I've seen the class  
method/singleton method relation serve as the "ah ha!" moment for  
many, many people grappling with singleton stuff in Ruby (even if it  
turns out to be a somewhat special case).

David

> **···**
>
> On Mon, 3 Oct 2005, Mark Volkmann wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Anonymous\_Coward](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Anonymous\_Coward](https://rubytalk.org/u/Anonymous_Coward)\
**Post date:** [3 October 2005 03:34 UTC](https://rubytalk.org/t/class-singleton-methods/21276/4 "2005-10-03T03:34:26Z")

</div>

David A. Black wrote:

> Hi --
> 
> > These seem to be identical. Are they?
> > 
> > class Foo  
> > def Foo.bar  
> > &nbsp;&nbsp;&nbsp;# some code  
> > end  
> > end
> > 
> > class Foo  
> > def self.bar  
> > &nbsp;&nbsp;&nbsp;# some code  
> > end  
> > end
> > 
> > If they are identical, is one form generally preferred over the other?
> 
> The advantage of the self version is that it might be a little easier  
> to maintain (e.g., if you change the name of the class).
> 
> > So a "class method" is the same as a "singleton method" on a Class object?
> 
> Basically, yes. I think the main difference is that they have a bit  
> of language-level "special case" status. There's the class\_methods  
> method, which makes them "official". Also, because of the way  
> classes' singleton classes work, a subclass can actually call the  
> singleton methods of its parent class:
> 
> &nbsp;&nbsp;class C  
> &nbsp;&nbsp;&nbsp;&nbsp;def self.x  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Calling x on #{self}"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;class D \< C  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;D.x # Calling x on D
> 
> As far as I know that's the only situation where an object can call  
> another object's singleton method.

Hm?

class Foo  
end

f = Foo.new

def f.foo()  
&nbsp;&nbsp;&nbsp;puts 'foo'  
end

f.foo

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... That may be enough to warrant a  
> different term for them. I have to say, though, I've seen the class  
> method/singleton method relation serve as the "ah ha!" moment for  
> many, many people grappling with singleton stuff in Ruby (even if it  
> turns out to be a somewhat special case).
> 
> David

E

> **···**
>
> > On Mon, 3 Oct 2005, Mark Volkmann wrote:

---

<div class="post-metadata">

**Author:** ![Mark\_Volkmann1](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@Mark\_Volkmann1](https://rubytalk.org/u/Mark_Volkmann1)\
**Post date:** [3 October 2005 13:30 UTC](https://rubytalk.org/t/class-singleton-methods/21276/5 "2005-10-03T13:30:33Z")

</div>

Did you mean the singleton\_methods method? I don't see a method named  
class\_methods anywhere.

> **···**
>
> On 10/2/05, David A. Black \<dblack@wobblini.net\> wrote:
> 
> > Hi --
> > 
> > On Mon, 3 Oct 2005, Mark Volkmann wrote:
> > 
> > \> These seem to be identical. Are they?  
> > \>  
> > \> class Foo  
> > \> def Foo.bar  
> > \> # some code  
> > \> end  
> > \> end  
> > \>  
> > \> class Foo  
> > \> def self.bar  
> > \> # some code  
> > \> end  
> > \> end  
> > \>  
> > \> If they are identical, is one form generally preferred over the other?
> > 
> > The advantage of the self version is that it might be a little easier  
> > to maintain (e.g., if you change the name of the class).
> > 
> > \> So a "class method" is the same as a "singleton method" on a Class object?
> > 
> > Basically, yes. I think the main difference is that they have a bit  
> > of language-level "special case" status. There's the class\_methods  
> > method, which makes them "official".
> 
> --  
> R. Mark Volkmann  
> Partner, Object Computing, Inc.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Seckar](https://avatars.discourse-cdn.com/v4/letter/n/b77776/32.png) [@Nicholas\_Seckar](https://rubytalk.org/u/Nicholas_Seckar)\
**Post date:** [3 October 2005 06:19 UTC](https://rubytalk.org/t/class-singleton-methods/21276/6 "2005-10-03T06:19:37Z")

</div>

ES wrote:

> > As far as I know that's the only situation where an object can call  
> > another object's singleton method.

Meaning that the singleton method of C is being called with self as D.

My apologies if I misinterpret you David.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [3 October 2005 12:18 UTC](https://rubytalk.org/t/class-singleton-methods/21276/7 "2005-10-03T12:18:26Z")

</div>

Hi --

> **···**
>
> On Mon, 3 Oct 2005, ES wrote:
> 
> > David A. Black wrote:
> > 
> > > As far as I know that's the only situation where an object can call  
> > > another object's singleton method.
> > 
> > Hm?
> > 
> > class Foo  
> > end
> > 
> > f = Foo.new
> > 
> > def f.foo()  
> > puts 'foo'  
> > end
> > 
> > f.foo
> 
> I think you must have misunderstood me. What I mean is: as far as I  
> know, class methods are the only per-object methods (defined as  
> obj.meth) that an object other than the object they were defined on  
> has in its method search path.
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [3 October 2005 13:57 UTC](https://rubytalk.org/t/class-singleton-methods/21276/8 "2005-10-03T13:57:45Z")

</div>

Hi --

> **···**
>
> On Mon, 3 Oct 2005, Mark Volkmann wrote:
> 
> > On 10/2/05, David A. Black \<dblack@wobblini.net\> wrote:
> > 
> > > Basically, yes. I think the main difference is that they have a bit  
> > > of language-level "special case" status. There's the class\_methods  
> > > method, which makes them "official".
> > 
> > Did you mean the singleton\_methods method? I don't see a method named  
> > class\_methods anywhere.
> 
> I meant class\_methods, and I was wrong. For some reason, I have for  
> five years been going repeatedly through the cycle of thinking there  
> is such a method, and then being reminded that there isn't, and then  
> thinking that there is. It's some kind of bizarre mental glitch.
> 
> I'm actually glad there isn't 🙂 And maybe this time I'll remember.
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net
