# Instance\_eval and class\_eval - names got mixed up?

**URL:** <https://www.ruby-forum.com/t/instance-eval-and-class-eval-names-got-mixed-up/131305>\
**Category:** Ruby\
**Created:** [March 7, 2008, 9:06pm UTC](https://www.ruby-forum.com/t/instance-eval-and-class-eval-names-got-mixed-up/131305 "2008-03-07T21:06:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [March 7, 2008, 9:06pm UTC](https://www.ruby-forum.com/t/instance-eval-and-class-eval-names-got-mixed-up/131305/1 "2008-03-07T21:06:53Z")

</div>

Going through the Ruby P.ming Language text Chapter 8 Reflection  
and MetaProgramming. Section 8.2.2 on page 270.

The following paragraph has me puzzled:

“Note the subtle but crucial difference between instance\_eval and  
class\_eval when the code being evaluated contains a method definition.  
instance\_eval defines singleton methods of the object (and this results  
in class methods when it is called on a class object). class\_eval  
defines regular instance methods.”

Is this not supposed to be the other way around? instance\_eval should  
define instance methods and class\_eval should define class methods. Is  
there a catch here? Ruby is supposed to be following principle of least  
surprise. I must say I am surprised here, or there is something quite  
profound that I don’t get.

Bharat

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [March 7, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/instance-eval-and-class-eval-names-got-mixed-up/131305/2 "2008-03-07T21:21:03Z")

</div>

On Fri, Mar 7, 2008 at 2:06 PM, Bharat R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Is this not supposed to be the other way around? instance\_eval should  
> define instance methods and class\_eval should define class methods. Is  
> there a catch here? Ruby is supposed to be following principle of least  
> surprise. I must say I am surprised here, or there is something quite  
> profound that I don’t get.
> 
> Bharat

It’s the use of the word “instance” here that causes the problem.  
Singleton methods are specific to a single instance of an object. An  
instance method refers to a method that’s available to _every_  
instance of a class upon their creation. Maybe it should be  
singleton\_eval and instance\_eval, but, see, that doesn’t really make  
sense, because that would imply eval being used within the singleton  
class of the object. One of those meta-programming language  
work-arounds I guess.

Todd

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [March 7, 2008, 9:28pm UTC](https://www.ruby-forum.com/t/instance-eval-and-class-eval-names-got-mixed-up/131305/3 "2008-03-07T21:28:41Z")

</div>

2008/3/7, Bharat R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Is this not supposed to be the other way around? instance\_eval should  
> define instance methods and class\_eval should define class methods. Is  
> there a catch here? Ruby is supposed to be following principle of least  
> surprise. I must say I am surprised here, or there is something quite  
> profound that I don’t get.

No, the book is correct. The “instance” in instance\_eval indicates that  
you are  
evaluating code in the context of one specific instance - if this  
instance is a class  
object, method definitions happen to define class methods.

instance\_eval is defined in Object, so you can use it on any object -  
nothing  
special about classes here, except that we call singleton methods on  
classes “class methods”.

class\_eval evaluates code as if it appeared between “class … end”.  
And a “def” between “class … end” defines instance methods of the  
class.

Stefan

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [March 7, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/instance-eval-and-class-eval-names-got-mixed-up/131305/4 "2008-03-07T22:09:23Z")

</div>

Thanks gentlemen for your responses. It still requires mental gyrations  
for me. I honestly think that this one got away from Matz.

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [March 7, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/instance-eval-and-class-eval-names-got-mixed-up/131305/5 "2008-03-07T22:21:11Z")

</div>

Bharat R. wrote:

> Thanks gentlemen for your responses. It still requires mental gyrations  
> for me. I honestly think that this one got away from Matz.

The confusion here is that you’re calling instance eval on a specific  
class. In ruby, classes are instances of the Class class (no, that is  
not a type). Methods added to your class (the instance of the class  
Class) are, in effect, added the the class definition. Below is an  
example of using class\_eval and instance\_eval against a simple class, so  
you might find this less confusing.

> > class Foo  
> > def hi  
> > puts “hi”  
> > end  
> > end  
> > =\> nil
> 
> > foo = Foo.new  
> > =\> #Foo:0x3aa250
> 
> > foo.instance\_eval do  
> > ?\> hi
> 
> > end  
> > hi  
> > =\> nil
> 
> > Foo.class\_eval do  
> > ?\> def howdy
> 
> > ```
> > puts "howdy"
> > 
> > ```
> > 
> > end  
> > end  
> > =\> nil
> 
> > foo.howdy  
> > howdy  
> > =\> nil

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [March 8, 2008, 1:46am UTC](https://www.ruby-forum.com/t/instance-eval-and-class-eval-names-got-mixed-up/131305/6 "2008-03-08T01:46:11Z")

</div>

Hi,

On Sat, Mar 8, 2008 at 8:09 AM, Bharat R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thanks gentlemen for your responses. It still requires mental gyrations  
> for me. I honestly think that this one got away from Matz.

Perhaps just try to remember that only classes have class\_eval, and only  
instances (i.e. everything) has instance\_eval. Hence, since all objects  
could have singleton methods, instance\_eval must add methods to the  
singleton class, whereas what you put in a `class_eval' block is similar to when you use the `class’ keyword itself.

Arlen

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [March 8, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/instance-eval-and-class-eval-names-got-mixed-up/131305/7 "2008-03-08T14:46:53Z")

</div>

Bharat R. wrote:

> I honestly think that this one got away from Matz.

So you’d want the names of the methods class\_eval and instance\_eval  
to be switched? So that doing “lala”.class\_eval would be allowed,  
but “lala”.instance\_eval would not? I’d find that a bit  
counter-intuitive  
to be honest.

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [March 8, 2008, 1:19am UTC](https://www.ruby-forum.com/t/instance-eval-and-class-eval-names-got-mixed-up/131305/8 "2008-03-08T01:19:30Z")

</div>

Bharat R. wrote:

> Going through the Ruby P.ming Language text Chapter 8 Reflection  
> and MetaProgramming. Section 8.2.2 on page 270.
> 
> The following paragraph has me puzzled:
> 
> “Note the subtle but crucial difference between instance\_eval and  
> class\_eval when the code being evaluated contains a method definition.  
> instance\_eval defines singleton methods of the object (and this results  
> in class methods when it is called on a class object). class\_eval  
> defines regular instance methods.”
> 
> Is this not supposed to be the other way around? instance\_eval should  
> define instance methods and class\_eval should define class methods. Is  
> there a catch here?

The catch is that a class is an instance. For a moment, forget that  
class\_eval even exists. Without that distraction, instance\_eval seems  
to operate consistently: 1) it creates singleton methods for 'normal  
'objects, e.g. mydog which is an instance of a Dog class, and 2) it  
creates singleton methods for objects which are instances of the class  
Class, i.e. all classes. It just so happens that a singleton method for  
a class object is known as ‘class method’.
