# Subclassing self

**URL:** <https://rubytalk.org/t/subclassing-self/7950>\
**Category:** ruby-talk\
**Created:** [9 September 2003 20:06 UTC](https://rubytalk.org/t/subclassing-self/7950 "2003-09-09T20:06:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gruby](https://avatars.discourse-cdn.com/v4/letter/g/58f4c7/32.png) [@Gruby](https://rubytalk.org/u/Gruby)\
**Post date:** [9 September 2003 20:06 UTC](https://rubytalk.org/t/subclassing-self/7950/1 "2003-09-09T20:06:26Z")

</div>

X-No-archive: yes

I’m a new ruby user. I have the Ruby Way book.

My question is, in what case would you use

class TheClass  
class \<\< self  
def foo

Couldn’t you just say

class TheClass  
def foo

?

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [9 September 2003 20:06 UTC](https://rubytalk.org/t/subclassing-self/7950/2 "2003-09-09T20:06:28Z")

</div>

No; that would define an _instance_ method called ‘foo’.  
The first example defines a _class_ method called ‘foo’.

That is, in the first example, you would call it like this:

```
TheClass.foo

```

And within the method, ‘self’ would refer to TheClass.

In your version, TheClass.foo doesn’t exist; instead, you  
have to instantiate the class and call foo on the object:

```
obj = TheClass.new
obj.foo

```

And within the method, ‘self’ will refer to obj.

Instead of using the class \<\< self notation, you could do this:

```
class TheClass
    def TheClass::foo

```

But that can get tedious when defining multiple class methods,  
plus you run the risk of missing a change if you rename the class  
later.

-Mark

> **···**
>
> On Tue, Sep 09, 2003 at 12:54:01PM -0700, John wrote:
> 
> > My question is, in what case would you use
> > 
> > class TheClass  
> > class \<\< self  
> > def foo
> > 
> > Couldn’t you just say
> > 
> > class TheClass  
> > def foo

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [9 September 2003 22:45 UTC](https://rubytalk.org/t/subclassing-self/7950/3 "2003-09-09T22:45:20Z")

</div>

Take a look at [http://www.rubygarden.org/ruby?ClassMethods](http://www.rubygarden.org/ruby?ClassMethods)

Gavin

> **···**
>
> On Wednesday, September 10, 2003, 6:06:26 AM, John wrote:
> 
> > X-No-archive: yes
> 
> > I’m a new ruby user. I have the Ruby Way book.
> 
> > My question is, in what case would you use
> 
> > class TheClass  
> > class \<\< self  
> > def foo
> 
> > Couldn’t you just say
> 
> > class TheClass  
> > def foo

---

<div class="post-metadata">

**Author:** ![Fredrik\_Jagenheim](https://avatars.discourse-cdn.com/v4/letter/f/c68b51/32.png) [@Fredrik\_Jagenheim](https://rubytalk.org/u/Fredrik_Jagenheim)\
**Post date:** [10 September 2003 21:00 UTC](https://rubytalk.org/t/subclassing-self/7950/4 "2003-09-10T21:00:00Z")

</div>

I was just shown:

```
class TheClass
    def self.foo

```

This way you don’t risk the missing a change when you rename the  
class. You still get the tedious typing though. 🙂

However, what does ‘class \<\< self’ really mean? Why is this one  
of the ‘obvious’ ways to define class methods?

No, I’m not being sarcastic, just realizing that I don’t know the ruby  
language as well as I would want to. To me it looks like we’re  
shifting the current class into an anonymous class, and how that can  
spell ‘class methods’ I really can’t see. 🙂

//F

> **···**
>
> On Wed, Sep 10, 2003 at 05:06:28AM +0900, Mark J. Reed wrote:
> 
> > Instead of using the class \<\< self notation, you could do this:
> > 
> > class TheClass  
> > def TheClass::foo
> > 
> > But that can get tedious when defining multiple class methods,  
> > plus you run the risk of missing a change if you rename the class  
> > later.

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [10 September 2003 22:26 UTC](https://rubytalk.org/t/subclassing-self/7950/5 "2003-09-10T22:26:52Z")

</div>

class \<\< obj opens the singleton class of obj. In a class context,  
self points to the Class object, therefore in

class A  
self # this is A  
class \<\< self # same as class \<\< A but needs not be changed if  
# if A is renamed  
# we’re in A’s singleton class  
end  
end

Class methods are in fact class singleton methods (ie. singleton methods  
of the object of class Class). You sometimes want to use the class \<\< self  
idiom to create attribute accessors for the class, etc:

class A  
class \<\< self  
attr\_accessor :foo  
end  
end

A.foo = 1  
A.foo # =\> 1

> **···**
>
> On Thu, Sep 11, 2003 at 06:00:00AM +0900, Fredrik Jagenheim wrote:
> 
> > On Wed, Sep 10, 2003 at 05:06:28AM +0900, Mark J. Reed wrote:
> > 
> > > Instead of using the class \<\< self notation, you could do this:
> > > 
> > > ```
> > > class TheClass
> > > def TheClass::foo
> > > 
> > > ```
> > > 
> > > But that can get tedious when defining multiple class methods,  
> > > plus you run the risk of missing a change if you rename the class  
> > > later.
> > 
> > I was just shown:
> > 
> > ```
> > class TheClass
> > def self.foo
> > 
> > ```
> > 
> > This way you don’t risk the missing a change when you rename the  
> > class. You still get the tedious typing though. 🙂
> > 
> > However, what does ‘class \<\< self’ really mean? Why is this one  
> > of the ‘obvious’ ways to define class methods?
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> Make it idiot-proof, and someone will breed a better idiot.  
> – Oliver Elphick

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [10 September 2003 22:33 UTC](https://rubytalk.org/t/subclassing-self/7950/6 "2003-09-10T22:33:11Z")

</div>

It’s not necessarily obvious; it’s an idiom, but it makes perfect  
sense once you know, and it involves core Ruby concepts, so it is  
worth knowing.

s = “Hi”  
class \<\< s  
def foo # Singleton method on object s  
5  
end  
end  
s.foo # → 5

s = String  
class \<\< s  
def foo # Singleton method on object String  
5  
end  
end  
s.foo # → 5  
String.foo # → 5

See [http://www.rubygarden.org/ruby?ClassMethods](http://www.rubygarden.org/ruby?ClassMethods) for more info.

Gavin

> **···**
>
> On Thursday, September 11, 2003, 7:00:00 AM, Fredrik wrote:
> 
> > On Wed, Sep 10, 2003 at 05:06:28AM +0900, Mark J. Reed wrote:
> > 
> > > Instead of using the class \<\< self notation, you could do this:
> > > 
> > > ```
> > > class TheClass
> > > def TheClass::foo
> > > 
> > > ```
> > > 
> > > But that can get tedious when defining multiple class methods,  
> > > plus you run the risk of missing a change if you rename the class  
> > > later.
> 
> > I was just shown:
> 
> > ```
> > class TheClass
> > def self.foo
> > 
> > ```
> 
> > This way you don’t risk the missing a change when you rename the  
> > class. You still get the tedious typing though. 🙂
> 
> > However, what does ‘class \<\< self’ really mean? Why is this one  
> > of the ‘obvious’ ways to define class methods?
