# What's different between self.method and class \<\< self?

**URL:** <https://rubytalk.org/t/whats-different-between-self-method-and-class-self/49747>\
**Category:** ruby-talk\
**Created:** [21 October 2008 07:23 UTC](https://rubytalk.org/t/whats-different-between-self-method-and-class-self/49747 "2008-10-21T07:23:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zhenning\_Guan](https://avatars.discourse-cdn.com/v4/letter/z/85e7bf/32.png) [@Zhenning\_Guan](https://rubytalk.org/u/Zhenning_Guan)\
**Post date:** [21 October 2008 07:23 UTC](https://rubytalk.org/t/whats-different-between-self-method-and-class-self/49747/1 "2008-10-21T07:23:20Z")

</div>

we can define a instance method in lots way.  
mostly, I saw people define like this:  
1.  
class A  
&nbsp;&nbsp;def self.class\_method  
&nbsp;&nbsp;#some code here....  
&nbsp;&nbsp;end  
end

2.  
class A  
&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;def class\_method  
&nbsp;&nbsp;&nbsp;&nbsp;#some code here....  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

> **···**
>
> ==  
> the question confused me is what's the advantage of doing this in 2?  
> they'are different? which is your choose?  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Patrick\_Doyle](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Patrick\_Doyle](https://rubytalk.org/u/Patrick_Doyle)\
**Post date:** [21 October 2008 12:20 UTC](https://rubytalk.org/t/whats-different-between-self-method-and-class-self/49747/2 "2008-10-21T12:20:16Z")

</div>

I asked this same question just a couple of weeks ago. The answers I was  
given were:

Method 2:  
1) Saves some typing  
2) Has a slightly different mechanism for looking up class constants, which  
99.99% of the time won't be noticed.

Other than that there are no differences between methods 1 & 2.

--wpd

> **···**
>
> On Tue, Oct 21, 2008 at 3:23 AM, Zhenning Guan \<g.zhen.ning@gmail.com\>wrote:
> 
> > we can define a instance method in lots way.  
> > mostly, I saw people define like this:  
> > 1.  
> > class A  
> > def self.class\_method  
> > #some code here....  
> > end  
> > end
> > 
> > 2.  
> > class A  
> > class \<\< self  
> > &nbsp;&nbsp;&nbsp;def class\_method  
> > &nbsp;&nbsp;&nbsp;#some code here....  
> > &nbsp;&nbsp;&nbsp;end  
> > end  
> > end
> > 
> > ==  
> > the question confused me is what's the advantage of doing this in 2?  
> > they'are different? which is your choose?

---

<div class="post-metadata">

**Author:** ![Shot\_Piotr\_Szotkowsk](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@Shot\_Piotr\_Szotkowsk](https://rubytalk.org/u/Shot_Piotr_Szotkowsk)\
**Post date:** [21 October 2008 17:46 UTC](https://rubytalk.org/t/whats-different-between-self-method-and-class-self/49747/3 "2008-10-21T17:46:40Z")

</div>

Zhenning Guan:

> we can define a instance method in lots way.  
> mostly, I saw people define like this:  
> 1.  
> class A  
> &nbsp;&nbsp;def self.class\_method  
> &nbsp;&nbsp;#some code here....  
> &nbsp;&nbsp;end  
> end

> 2.  
> class A  
> &nbsp;&nbsp;class \<\< self  
> &nbsp;&nbsp;&nbsp;&nbsp;def class\_method  
> &nbsp;&nbsp;&nbsp;&nbsp;#some code here....  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end

> the question confused me is what's the advantage of doing this in 2?

You can work on the singleton’s properties with attr\_\* methods:

class Example  
&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :prop  
&nbsp;&nbsp;end  
end

(With the first approach you’d have to define self.prop and  
self.prop= to be able to write code like Example.prop = 'value'.)

-- Shot

> **···**
>
> --  
> Damn, I need to rant, not whimper. Someone bring me a user. -- Wayne Pascoe

---

<div class="post-metadata">

**Author:** ![Zhenning\_Guan](https://avatars.discourse-cdn.com/v4/letter/z/85e7bf/32.png) [@Zhenning\_Guan](https://rubytalk.org/u/Zhenning_Guan)\
**Post date:** [21 October 2008 12:26 UTC](https://rubytalk.org/t/whats-different-between-self-method-and-class-self/49747/4 "2008-10-21T12:26:34Z")

</div>

Patrick Doyle wrote:

> **···**
>
> > On Tue, Oct 21, 2008 at 3:23 AM, Zhenning Guan \> \<g.zhen.ning@gmail.com\>wrote:
> > 
> > > class A  
> > > they'are different? which is your choose?
> > 
> > I asked this same question just a couple of weeks ago. The answers I  
> > was  
> > given were:
> > 
> > Method 2:  
> > 1) Saves some typing  
> > 2) Has a slightly different mechanism for looking up class constants,  
> > which  
> > 99.99% of the time won't be noticed.
> > 
> > Other than that there are no differences between methods 1 & 2.
> > 
> > --wpd
> 
> what's the meaning of saves some typing???  
> can you give me a example?  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Shot\_Piotr\_Szotkowsk](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@Shot\_Piotr\_Szotkowsk](https://rubytalk.org/u/Shot_Piotr_Szotkowsk)\
**Post date:** [21 October 2008 18:22 UTC](https://rubytalk.org/t/whats-different-between-self-method-and-class-self/49747/5 "2008-10-21T18:22:10Z")

</div>

Shot (Piotr Szotkowski):

> (With the first approach you’d have to define self.prop and  
> self.prop= to be able to write code like Example.prop = 'value'.)

…or use something like Rails’ cattr\_\* accessors – but they work on  
class variables (as opposed to instance variables of the singleton.)

-- Shot

> **···**
>
> --  
> Life being what it is, one dreams of revenge. -- Paul Gauguin

---

<div class="post-metadata">

**Author:** ![Patrick\_Doyle](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Patrick\_Doyle](https://rubytalk.org/u/Patrick_Doyle)\
**Post date:** [21 October 2008 12:32 UTC](https://rubytalk.org/t/whats-different-between-self-method-and-class-self/49747/6 "2008-10-21T12:32:01Z")

</div>

> what's the meaning of saves some typing???  
> can you give me a example?  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> Instead of having to type

def self.method  
&nbsp;&nbsp;&nbsp;blah  
&nbsp;&nbsp;&nbsp;blah  
&nbsp;&nbsp;&nbsp;blah  
end

you can type (within the class \<\< self clause)

def method  
&nbsp;&nbsp;blah  
&nbsp;&nbsp;blah  
&nbsp;&nbsp;blah  
end

I didn't say it would save you a \_lot\_ of typing, just some 🙂

--wpd
