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

**URL:** <https://www.ruby-forum.com/t/whats-different-between-self-method-and-class-self/151424>\
**Category:** Ruby\
**Created:** [October 21, 2008, 9:24am UTC](https://www.ruby-forum.com/t/whats-different-between-self-method-and-class-self/151424 "2008-10-21T09:24:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [October 21, 2008, 9:24am UTC](https://www.ruby-forum.com/t/whats-different-between-self-method-and-class-self/151424/1 "2008-10-21T09:24:35Z")

</div>

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

1. 

class A  
class \<\< self  
def class\_method  
#some code here…  
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:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [October 21, 2008, 2:22pm UTC](https://www.ruby-forum.com/t/whats-different-between-self-method-and-class-self/151424/2 "2008-10-21T14:22:32Z")

</div>

On Tue, Oct 21, 2008 at 3:23 AM, Zhenning G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [October 21, 2008, 2:27pm UTC](https://www.ruby-forum.com/t/whats-different-between-self-method-and-class-self/151424/3 "2008-10-21T14:27:53Z")

</div>

Patrick D. wrote:

> On Tue, Oct 21, 2008 at 3:23 AM, Zhenning G.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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?

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [October 21, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/whats-different-between-self-method-and-class-self/151424/4 "2008-10-21T14:33:42Z")

</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/).
> 
> Instead of having to type

def self.method  
blah  
blah  
blah  
end

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

def method  
blah  
blah  
blah  
end

I didn’t say it would save you a _lot_ of typing, just some 🙂

–wpd

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [October 21, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/whats-different-between-self-method-and-class-self/151424/5 "2008-10-21T22:09:56Z")

</div>

Shot (Piotr S.):

> (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

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [October 21, 2008, 9:38pm UTC](https://www.ruby-forum.com/t/whats-different-between-self-method-and-class-self/151424/6 "2008-10-21T21:38:38Z")

</div>

Zhenning G.:

> 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

> 1. 
> 
> class A  
> class \<\< self  
> def class\_method  
> #some code here…  
> end  
> 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  
class \<\< self  
attr\_accessor :prop  
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
