# Making sense of the various Ruby "eval"s

**URL:** <https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384>\
**Category:** ruby-talk\
**Created:** [12 May 2006 15:09 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384 "2006-05-12T15:09:59Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eli\_Bendersky](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@Eli\_Bendersky](https://rubytalk.org/u/Eli_Bendersky)\
**Post date:** [12 May 2006 15:09 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384/1 "2006-05-12T15:09:59Z")

</div>

Hi all,

Unlike other dynamic languages I'm familiar with (Perl, Lisp), Ruby has  
more than one form of 'eval', which is quite confusing for Ruby nubies  
like myself. I'm trying to make some sense of all the possibilities:

- Kernel#eval -

This one I (almost) understand. It behaves like Perl's eval, with the  
difference that it can take a 'binding' which is another Object, or  
Proc. In case of the Object I understand that the code 'eval'ed can  
access the Object's instance vars. But how does that work with a Proc as  
a binding ?

- Object#instance\_eval -

Is obj.instance\_eval("blah") exactly the same as eval("blah", obj) ? If  
the answer to this question is positive, then the string version of  
instance\_eval is clear. But what about its block version ? How does that  
work ? Are the contents of the block just treated as a string ?

- Module#class\_eval -

With its synonym module\_eval: very similar to Object#instance\_eval, just  
that all methods are added to a class and not to an object, right ?

Thanks in advance

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [12 May 2006 15:31 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384/2 "2006-05-12T15:31:00Z")

</div>

> - Kernel#eval -

it's the evil eval, forget it 🙂

> - Object#instance\_eval -

> Is obj.instance\_eval("blah") exactly the same as eval("blah", obj) ?

You can give only a Proc/Binding to the evil eval

> work ? Are the contents of the block just treated as a string ?

no, the block is executed with self = object

> - Module#class\_eval -

moulon% cat b.rb  
#!/usr/bin/ruby  
class A  
end

A.instance\_eval {  
&nbsp;&nbsp;&nbsp;p self  
&nbsp;&nbsp;&nbsp;def a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "A::a"  
&nbsp;&nbsp;&nbsp;end  
}

A.module\_eval {  
&nbsp;&nbsp;&nbsp;p self  
&nbsp;&nbsp;&nbsp;def a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "A#a"  
&nbsp;&nbsp;&nbsp;end  
}

A.a  
A.new.a  
moulon%

moulon% ./b.rb  
A  
A  
A#a  
moulon%

Guy Decoux

> **···**
>
> A::a

---

<div class="post-metadata">

**Author:** ![Eli\_Bendersky](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@Eli\_Bendersky](https://rubytalk.org/u/Eli_Bendersky)\
**Post date:** [12 May 2006 16:02 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384/3 "2006-05-12T16:02:12Z")

</div>

ts wrote:

> \> - Kernel#eval -
> 
> it's the evil eval, forget it 🙂

Forget Kernel#eval or the string version of any #eval ? The first maybe,  
but the second is what allows code generation on-the-fly.

> \> - Object#instance\_eval -
> 
> \> Is obj.instance\_eval("blah") exactly the same as eval("blah", obj) ?
> 
> You can give only a Proc/Binding to the evil eval
> 
> \> work ? Are the contents of the block just treated as a string ?
> 
> no, the block is executed with self = object

And in the string version, what is 'self' ?  
I'm struggling to fully understand the difference between the block and  
the string versions ?

> \> - Module#class\_eval -
> 
> moulon% cat b.rb  
> #!/usr/bin/ruby  
> class A  
> end

\<snip\>

OK I think I understand: instance\_eval adds instance methods to a class,  
and class/module\_eval adds class methods to a class. But also  
instance\_eval can be called on any object, but class\_eval can't (it can  
be called only on class / module objects). Correct ?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [12 May 2006 16:09 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384/4 "2006-05-12T16:09:47Z")

</div>

> I'm struggling to fully understand the difference between the block and  
> the string versions ?

the string version is evil 🙂

> OK I think I understand: instance\_eval adds instance methods to a class,

instance\_eval add singleton methods, this is why you can use it with any  
objects (not only class, modules).

> and class/module\_eval adds class methods to a class. But also

no, add instance method for a class. This is why you can use it only with  
a class, module.

> instance\_eval can be called on any object, but class\_eval can't (it can  
> be called only on class / module objects). Correct ?

yes.

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Eli\_Bendersky](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@Eli\_Bendersky](https://rubytalk.org/u/Eli_Bendersky)\
**Post date:** [13 May 2006 05:52 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384/5 "2006-05-13T05:52:13Z")

</div>

ts wrote:

> \> I'm struggling to fully understand the difference between the block  
> and  
> \> the string versions ?
> 
> the string version is evil 🙂

It may be evil but it is necessary. Correct me if I am wrong, but this  
is how I see it - the string version of eval is the only way to  
differentiate between "evaluation time" and "execution time". Example:

name = '"Mike"'  
evaltime = eval "lambda {name = 'Jane'; puts #{name}}";  
runtime = eval "lambda {name = 'Jane'; puts name}";

evaltime.call  
runtime.call

==\> Mike  
==\> Jane

Unless I'm misunderstanding something, this can be only done with the  
string version of eval. Additionally, stitching pieces of code together,  
like this:

var = '"Jane"'  
estr = "lambda {name =" + var + "; puts name}"

runtime = eval estr  
runtime.call

Can also be done only with the string version.

The way I see it, the block version is simply a convenience, making the  
code cleaner. It is similar to wrapping a string into a single  
(non-interpolating) quote:

class Ab  
end

boor = Ab.new

boor.instance\_eval {  
&nbsp;&nbsp;&nbsp;p "using block"  
&nbsp;&nbsp;&nbsp;def a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Ab::a"  
&nbsp;&nbsp;&nbsp;end  
}

boor.instance\_eval %q{  
&nbsp;&nbsp;p "using %q"  
&nbsp;&nbsp;def b  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Ab::b"  
&nbsp;&nbsp;end  
}

boor.a  
boor.b

==\> "using block"  
==\> "using %q"  
==\> Ab::a  
==\> Ab::b

What have I missed ?

Thanks in advance.  
Eli

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [13 May 2006 08:10 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384/6 "2006-05-13T08:10:20Z")

</div>

> What have I missed ?

The evilness of eval.

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford4](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Ross\_Bamford4](https://rubytalk.org/u/Ross_Bamford4)\
**Post date:** [13 May 2006 17:24 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384/7 "2006-05-13T17:24:11Z")

</div>

As well as the difference in when the code is parsed mentioned in  
another post, it's probably worth noting that string eval is slower than  
block eval:

$ cat eval\_bench2.rb  
require 'benchmark'  
o = Object.new  
Benchmark.bm(10) do |b|  
&nbsp;&nbsp;b.report("eval str:") { 10000.times {o.instance\_eval '10.times { |i| l = 300 \* i }' }}  
&nbsp;&nbsp;b.report("eval blk:") { 10000.times {o.instance\_eval { 10.times { |i| l = 300 \* i }} }}  
end

$ ruby -v eval\_bench2.rb  
ruby 1.8.4 (2005-12-24) [i686-linux]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
eval str: 0.340000 0.000000 0.340000 ( 0.416134)  
eval blk: 0.160000 0.000000 0.160000 ( 0.203212)

In yarv, the difference is even more marked, and from what I can gather  
it's likely to stay that way:

$ ruby-yarv -v eval\_bench2.rb  
ruby 2.0.0 (Base: Ruby 1.9.0 2006-04-08) [i686-linux]  
YARVCore 0.4.0 Rev: 497 (2006-05-07) [opts:]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
eval str: 0.820000 0.020000 0.840000 ( 0.836336)  
eval blk: 0.060000 0.000000 0.060000 ( 0.105170)

> **···**
>
> On Sat, 2006-05-13 at 14:52 +0900, Eli Bendersky wrote:
> 
> > ts wrote:
> 
> > \>  
> > \> \> I'm struggling to fully understand the difference between the block  
> > \> and  
> > \> \> the string versions ?  
> > \>  
> > \> the string version is evil 🙂  
> > \>  
> > [...]  
> > The way I see it, the block version is simply a convenience, making the  
> > code cleaner. It is similar to wrapping a string into a single  
> > (non-interpolating) quote:
> 
> --  
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk

---

<div class="post-metadata">

**Author:** ![Eli\_Bendersky](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@Eli\_Bendersky](https://rubytalk.org/u/Eli_Bendersky)\
**Post date:** [13 May 2006 08:28 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384/8 "2006-05-13T08:28:19Z")

</div>

ts wrote:

> \> What have I missed ?
> 
> The evilness of eval.
> 
> Guy Decoux

Guy,

I would really appreciate a serious answer. The Ruby documentation is  
bad, there's no need to tell you that, and I'm trying to understand  
things in depth, hence my questions.

I understand that eval can be mis-used, like many other advanced  
features.

However, I much prefer a serious to-the-point answer. You have cleared  
out a lot for me, and I thank you for that. What is left is my last  
question:

Is block eval the same as single-quoted-string eval ?

Thanks in advance

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Pat\_Maddox1](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@Pat\_Maddox1](https://rubytalk.org/u/Pat_Maddox1)\
**Post date:** [14 May 2006 06:42 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384/9 "2006-05-14T06:42:47Z")

</div>

I genuinely have no clue what the difference is between class\_eval and  
instance\_eval. Check out this quick irb session:

irb(main):111:0\> class Foo; end  
=\> nil  
irb(main):112:0\> Foo.instance\_eval { define\_method(:foo) { "foo" } }  
=\> #\<Proc:0x00318b00@(irb):112\>  
irb(main):113:0\> Foo.foo  
NoMethodError: undefined method `foo' for Foo:Class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):113  
irb(main):114:0\> Foo.new.foo  
=\> "foo"  
irb(main):115:0\> class Bar; end  
=\> nil  
irb(main):116:0\> Bar.class\_eval { define\_method(:bar) { "bar" } }  
=\> #\<Proc:0x003078a0@(irb):116\>  
irb(main):117:0\> Bar.bar  
NoMethodError: undefined method `bar' for Bar:Class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):117  
irb(main):118:0\> Bar.new.bar  
=\> "bar"

I would think that since I used class\_eval in one and instance\_eval in  
the other, SOMETHING should be different..

Pat

> **···**
>
> from :0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernandez](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Mauricio\_Fernandez](https://rubytalk.org/u/Mauricio_Fernandez)\
**Post date:** [13 May 2006 09:03 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384/10 "2006-05-13T09:03:21Z")

</div>

$ cat string.rb  
def foo  
&nbsp;&nbsp;instance\_eval %{  
&nbsp;&nbsp;&nbsp;&nbsp;387 8sbf fy"(/·%" /·/% "· ·· sdug sdf  
&nbsp;&nbsp;}  
end

puts "I can't foresee any problem."

# much later, in code far, far away  
#foo

$ ruby string.rb  
I can't foresee any problem.  
$ cat block.rb  
def foo  
&nbsp;&nbsp;instance\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;387 8sbf fy"(/·%" /·/% "· ·· sdug sdf  
&nbsp;&nbsp;end  
end

puts "I can't foresee any problem."

# much later, in code far, far away  
#foo

$ ruby block.rb  
block.rb:4: syntax error, unexpected tINTEGER, expecting kEND  
&nbsp;&nbsp;&nbsp;&nbsp;387 8sbf fy"(/·%" /·/% "· ·· sdug sdf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
block.rb:4: Invalid char `\302' in expression  
block.rb:4: Invalid char `\267' in expression  
block.rb:4: unterminated string meets end of file  
block.rb:4: syntax error, unexpected tSTRING\_END, expecting tSTRING\_CONTENT or tREGEXP\_END or tSTRING\_DBEG or tSTRING\_DVAR

Another difference:

$ cat eval.rb  
def foo\_string(str)  
&nbsp;&nbsp;eval str  
&nbsp;&nbsp;instance\_eval "var"  
end

def foo\_block(str)  
&nbsp;&nbsp;eval str  
&nbsp;&nbsp;instance\_eval { var }  
end

puts foo\_string("var = 'some value'")  
puts foo\_block("var = 'some value'")

$ ruby eval.rb  
some value  
eval.rb:8:in `foo_block': undefined local variable or method `var' for main:Object (NameError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from eval.rb:8:in `foo\_block'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from eval.rb:12

> **···**
>
> On Sat, May 13, 2006 at 05:28:19PM +0900, Eli Bendersky wrote:
> 
> > ts wrote:
> 
> > \>  
> > \> \> What have I missed ?  
> > \>  
> > \> The evilness of eval.  
> > \>  
> > \> Guy Decoux
> > 
> > Guy,
> > 
> > I would really appreciate a serious answer. The Ruby documentation is  
> > bad, there's no need to tell you that, and I'm trying to understand  
> > things in depth, hence my questions.
> > 
> > I understand that eval can be mis-used, like many other advanced  
> > features.
> > 
> > However, I much prefer a serious to-the-point answer. You have cleared  
> > out a lot for me, and I thank you for that. What is left is my last  
> > question:
> > 
> > Is block eval the same as single-quoted-string eval ?
> 
> --  
> Mauricio Fernandez - [http://eigenclass.org](http://eigenclass.org) - singular Ruby

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [14 May 2006 07:15 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384/11 "2006-05-14T07:15:53Z")

</div>

Hi --

> I genuinely have no clue what the difference is between class\_eval and  
> instance\_eval. Check out this quick irb session:
> 
> irb(main):111:0\> class Foo; end  
> =\> nil  
> irb(main):112:0\> Foo.instance\_eval { define\_method(:foo) { "foo" } }  
> =\> #\<Proc:0x00318b00@(irb):112\>  
> irb(main):113:0\> Foo.foo  
> NoMethodError: undefined method `foo' for Foo:Class  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):113  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0  
> irb(main):114:0\> Foo.new.foo  
> =\> "foo"  
> irb(main):115:0\> class Bar; end  
> =\> nil  
> irb(main):116:0\> Bar.class\_eval { define\_method(:bar) { "bar" } }  
> =\> #\<Proc:0x003078a0@(irb):116\>  
> irb(main):117:0\> Bar.bar  
> NoMethodError: undefined method `bar' for Bar:Class  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):117  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0  
> irb(main):118:0\> Bar.new.bar  
> =\> "bar"
> 
> I would think that since I used class\_eval in one and instance\_eval in  
> the other, SOMETHING should be different..

Fear not; it is 🙂 Look at what happens when you use def:

> > class C; end

=\> nil

> > C.instance\_eval { def x; 1; end } # singleton method on C

=\> nil

> > C.x

=\> 1

> > C.new.x

NoMethodError: undefined method `x' for #\<C:0x35b16c\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4

> > C.class\_eval { def y; 1; end } # instance method of C

=\> nil

> > C.y

NoMethodError: undefined method `y' for C:Class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6

> > C.new.y

=\> 1

C.instance\_eval handles the business of C itself, as an object. You'd  
get similar results with any\_object.instance\_eval { def x; 1; end }

C.class\_eval takes you into a class definition block for C, where def  
is an instance method of C. So it's much more class (and module)  
specific in its engineering.

David

> **···**
>
> On Sun, 14 May 2006, Pat Maddox wrote:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0
> 
> --  
> David A. Black (dblack@wobblini.net)  
> \* Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))  
> &nbsp;&nbsp;&nbsp;\> Ruby and Rails consultancy and training  
> \* Author of "Ruby for Rails" from Manning Publications!  
> &nbsp;&nbsp;&nbsp;\> [Ruby for Rails](http://www.manning.com/black)

---

<div class="post-metadata">

**Author:** ![Eli\_Bendersky](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@Eli\_Bendersky](https://rubytalk.org/u/Eli_Bendersky)\
**Post date:** [13 May 2006 09:27 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384/12 "2006-05-13T09:27:26Z")

</div>

Mauricio Fernandez wrote:

> > Is block eval the same as single-quoted-string eval ?
> 
> $ cat string.rb  
> def foo  
> &nbsp;&nbsp;instance\_eval %{  
> &nbsp;&nbsp;&nbsp;&nbsp;387 8sbf fy"(/�%" /�/% "� �� sdug sdf  
> &nbsp;&nbsp;}  
> end

\<snip\>

> Another difference:
> 
> $ cat eval.rb  
> def foo\_string(str)  
> &nbsp;&nbsp;eval str  
> &nbsp;&nbsp;instance\_eval "var"  
> end
> 
> def foo\_block(str)  
> &nbsp;&nbsp;eval str  
> &nbsp;&nbsp;instance\_eval { var }  
> end
> 
> puts foo\_string("var = 'some value'")  
> puts foo\_block("var = 'some value'")

\<snip\>

OK, thanks - this I understand. The block version of eval checks the  
block at compile time for errors, while the string version does not.  
This is another plus for the block version, and generally I agree that  
it should be used over the string version whenever that is possible.

The string version, however, has its uses, and can do things the block  
version can't, as I have shown in my examples. Such uses should be well  
thought-out and kept to the minimum, of course.

Eli

> **···**
>
> > On Sat, May 13, 2006 at 05:28:19PM +0900, Eli Bendersky wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Pat\_Maddox1](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@Pat\_Maddox1](https://rubytalk.org/u/Pat_Maddox1)\
**Post date:** [14 May 2006 11:05 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384/13 "2006-05-14T11:05:18Z")

</div>

Okay so I guess the problem is that I'm using define\_method? I don't  
understand the difference between define\_method and def then.

define\_method: "Defines an instance method in the receiver...This  
block is evaluated using instance\_eval..."

After first reading that, I thought it explained why there was no  
difference between using instance\_eval or class\_eval with  
define\_method, because define\_method uses instance\_eval itself.  
However isn't self different when I use instance\_eval vs class\_eval,  
so then the inner instance\_eval call would operate on a different  
self, making the results different?

Pat

> **···**
>
> On 5/14/06, dblack@wobblini.net \<dblack@wobblini.net\> wrote:
> 
> > Hi --
> > 
> > On Sun, 14 May 2006, Pat Maddox wrote:
> > 
> > \> I genuinely have no clue what the difference is between class\_eval and  
> > \> instance\_eval. Check out this quick irb session:  
> > \>  
> > \> irb(main):111:0\> class Foo; end  
> > \> =\> nil  
> > \> irb(main):112:0\> Foo.instance\_eval { define\_method(:foo) { "foo" } }  
> > \> =\> #\<Proc:0x00318b00@(irb):112\>  
> > \> irb(main):113:0\> Foo.foo  
> > \> NoMethodError: undefined method `foo' for Foo:Class  
> > \> from (irb):113  
> > \> from :0  
> > \> irb(main):114:0\> Foo.new.foo  
> > \> =\> "foo"  
> > \> irb(main):115:0\> class Bar; end  
> > \> =\> nil  
> > \> irb(main):116:0\> Bar.class\_eval { define\_method(:bar) { "bar" } }  
> > \> =\> #\<Proc:0x003078a0@(irb):116\>  
> > \> irb(main):117:0\> Bar.bar  
> > \> NoMethodError: undefined method `bar' for Bar:Class  
> > \> from (irb):117  
> > \> from :0  
> > \> irb(main):118:0\> Bar.new.bar  
> > \> =\> "bar"  
> > \>  
> > \> I would think that since I used class\_eval in one and instance\_eval in  
> > \> the other, SOMETHING should be different..
> > 
> > Fear not; it is 🙂 Look at what happens when you use def:
> > 
> > \>\> class C; end  
> > =\> nil  
> > \>\> C.instance\_eval { def x; 1; end } # singleton method on C  
> > =\> nil  
> > \>\> C.x  
> > =\> 1  
> > \>\> C.new.x  
> > NoMethodError: undefined method `x' for #\<C:0x35b16c\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0  
> > \>\> C.class\_eval { def y; 1; end } # instance method of C  
> > =\> nil  
> > \>\> C.y  
> > NoMethodError: undefined method `y' for C:Class  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0  
> > \>\> C.new.y  
> > =\> 1
> > 
> > C.instance\_eval handles the business of C itself, as an object. You'd  
> > get similar results with any\_object.instance\_eval { def x; 1; end }
> > 
> > C.class\_eval takes you into a class definition block for C, where def  
> > is an instance method of C. So it's much more class (and module)  
> > specific in its engineering.
> > 
> > David
> > 
> > --  
> > David A. Black (dblack@wobblini.net)  
> > \* Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))  
> > &nbsp;&nbsp;&nbsp;\> Ruby and Rails consultancy and training  
> > \* Author of "Ruby for Rails" from Manning Publications!  
> > &nbsp;&nbsp;&nbsp;\> [Ruby for Rails](http://www.manning.com/black)

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [14 May 2006 12:05 UTC](https://rubytalk.org/t/making-sense-of-the-various-ruby-eval-s/27384/14 "2006-05-14T12:05:01Z")

</div>

> define\_method: "Defines an instance method in the receiver...This  
> block is evaluated using instance\_eval..."

Well, the block is not really evaluated with instance\_eval

moulon% cat b.rb  
#!/usr/bin/ruby  
class A  
&nbsp;&nbsp;&nbsp;def create\_method(name, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class.send(:define\_method, name, &block)  
&nbsp;&nbsp;&nbsp;end  
end

class B  
&nbsp;&nbsp;&nbsp;def a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A.new.create\_method(:a) { def b() end }  
&nbsp;&nbsp;&nbsp;end  
end

B.new.a  
A.new.a  
A.new.b  
moulon%

if the block '{ def b() end }' is evaluated with instance\_eval, ruby will  
create a singleton method, otherwise my script will work.

moulon% ./b.rb  
moulon%

Guy Decoux
