# "self.class" vs. "(class \<\< self; self; end)"

**URL:** <https://www.ruby-forum.com/t/self-class-vs-class-self-self-end/96623>\
**Category:** Ruby\
**Created:** [January 24, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/self-class-vs-class-self-self-end/96623 "2006-01-24T16:51:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 24, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/self-class-vs-class-self-self-end/96623/1 "2006-01-24T16:51:38Z")

</div>

Is there any difference between these?

def test1()  
msg = ‘bar’  
self.class.class\_eval do  
define\_method(‘foo’) { msg }  
end  
end

def test2  
msg = ‘baz’  
(class \<\< self; self; end).class\_eval do  
define\_method(‘foo’) { msg }  
end  
end

test1  
puts foo

test2  
puts foo

As far as I can tell, no?

If that’s the case, is there any reason the second form seems so much  
more common?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 24, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/self-class-vs-class-self-self-end/96623/2 "2006-01-24T17:10:02Z")

</div>

removed\_email\_address@domain.invalid wrote:

Yes there is a difference.  
In the first example You simply reopen a class a define a method for it.  
In the second example You reopen an eigenclass (which is a singleton  
instance of class object) and then define a method for it.

Ruby returns last evaluated statement which in this case ‘(class \<\<  
self; self; end)’ is eigenclass…

The eigneclass pattern (a metaclass) is a method of creating single  
class instances per instances of them. (I dont know if I made it clear  
🙂 )

consider:

def test1()  
msg = ‘bar’  
self.class.class\_eval do  
p self  
define\_method(‘foo’) { msg }  
end  
end

def test2  
msg = ‘baz’  
(class \<\< self; self; end).class\_eval do  
p self  
define\_method(‘foo’) { msg }  
end  
end

test1  
p foo

test2  
p foo

output:

Object  
“bar”  
#\<Class:#Object:0x2859258\>  
“baz”

lopex

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 24, 2006, 5:28pm UTC](https://www.ruby-forum.com/t/self-class-vs-class-self-self-end/96623/3 "2006-01-24T17:28:17Z")

</div>

How does one recognize when to use these techniques?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 24, 2006, 5:34pm UTC](https://www.ruby-forum.com/t/self-class-vs-class-self-self-end/96623/4 "2006-01-24T17:34:21Z")

</div>

> In the second example You reopen an eigenclass (which is a singleton  
> instance of class object) and then define a method for it.  
> What exactly do you mean by eigenclass? is it the same as an  
> anonymous class created by class\<\<obj syntax?

> Ruby returns last evaluated statement which in this case ‘(class \<\<  
> self; self; end)’ is eigenclass…
> 
> The eigneclass pattern (a metaclass) is a method of creating single  
> class instances per instances of them. (I dont know if I made it clear 🙂 )

Is this correct.In the first method,we add a method to the class and so  
all objects of this class instantiated from the point after calling  
test1 will have the method ‘foo’ .What about objects created before  
this point?  
But in the second case ,only the object on which we call test1 will  
have the method foo defined.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 24, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/self-class-vs-class-self-self-end/96623/5 "2006-01-24T17:49:47Z")

</div>

Vivek wrote:

> What exactly do you mean by eigenclass? is it the same as an  
> anonymous class created by class\<\<obj syntax?

Yes (but actually I haven’t found any eigenclass definintions on the  
web, not even on [http://eigenclass.org/](http://eigenclass.org/))

> Is this correct.In the first method,we add a method to the class and so  
> all objects of this class instantiated from the point after calling  
> test1 will have the method ‘foo’ .What about objects created before  
> this point?

They will respond to those methods (since THEIR class has been changed):

class A  
def meth1  
end  
end

a=A.new

class A  
def meth2  
end  
end

class A  
define\_method :meth3 do  
end  
end

a.meth2  
a.meth3

lopex

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 25, 2006, 4:55am UTC](https://www.ruby-forum.com/t/self-class-vs-class-self-self-end/96623/6 "2006-01-25T04:55:57Z")

</div>

The [eigenclass.org](http://eigenclass.org) even has a favicon.ico which looks like '\<\< ’ 🙂

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 25, 2006, 12:54am UTC](https://www.ruby-forum.com/t/self-class-vs-class-self-self-end/96623/7 "2006-01-25T00:54:40Z")

</div>

On Jan 24, 2006, at 10:49 AM, Marcin MielÅ¼yÅ?ski wrote:

> Vivek wrote:
> 
> > What exactly do you mean by eigenclass? is it the same as an  
> > anonymous class created by class\<\<obj syntax?
> 
> Yes (but actually I haven’t found any eigenclass definintions on  
> the web, not even on [http://eigenclass.org/](http://eigenclass.org/))

The expression:

```
(class <<target; self; end)

```

evaluates to an instance of Class that has the unique  
property that its instance methods are only available to  
the object referenced by ‘target’.

Traditionally this class has been called target’s ‘singleton class’.

There has been some community frustration with that particular name  
due to other meanings of ‘singleton’ as well as the existence in the  
standard library of a class with the name ‘Singleton’; both of which  
have nothing to do with the expression above. Matz has also hinted  
that this feature might not be class based in the future, which would of  
course make the term ‘singleton class’ even more confusing. So  
this very useful concept floats around in nomenclature-limbo at the  
moment.

Alternative terms have been suggested. Eigenclass is one of them.

Gary W.
