# Class\_eval used inline vs within class definition

**URL:** <https://www.ruby-forum.com/t/class-eval-used-inline-vs-within-class-definition/58510>\
**Category:** Ruby\
**Created:** [April 22, 2006, 11:30am UTC](https://www.ruby-forum.com/t/class-eval-used-inline-vs-within-class-definition/58510 "2006-04-22T11:30:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 22, 2006, 11:30am UTC](https://www.ruby-forum.com/t/class-eval-used-inline-vs-within-class-definition/58510/1 "2006-04-22T11:30:09Z")

</div>

I’m reading the Ruby for Rails book, and on page 350 it has the example

irb(main):001:0\> var = “initialized variable”  
=\> “initialized variable”  
irb(main):002:0\> class C; end  
=\> nil  
irb(main):003:0\> C.class\_eval { define\_method(“talk”) { puts var } }  
=\> #Proc:0x00355c30@:3(irb)  
irb(main):004:0\> C.new.talk  
initialized variable  
=\> nil

So I wanted to see if I could do that within the class. I’ve tried  
the following approach:  
irb(main):005:0\> class A  
irb(main):006:1\> class\_eval { define\_method(“talk”) { puts var } }  
irb(main):007:1\> end  
=\> #Proc:0x0033f0ac@:6(irb)  
irb(main):008:0\> A.new.talk  
NameError: undefined local variable or method `var' for #<A:0x337c30> from (irb):6:in`talk’  
from (irb):8  
from :0

I’ve also tried it with self.class\_eval within the class definition.  
Can someone explain to me what the difference is between the inline  
code example, and specifying it inside the class? Obviously they’re  
not equivalent, otherwise my attempt would work…I just don’t  
understand what the difference is.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 22, 2006, 1:55pm UTC](https://www.ruby-forum.com/t/class-eval-used-inline-vs-within-class-definition/58510/2 "2006-04-22T13:55:24Z")

</div>

On 4/22/06, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can someone explain to me what the difference is between the inline  
> code example, and specifying it inside the class?

A class definition introduces a new scope (similar to def) so for your  
example to work you need to define the local variable ~within~ the  
class definition for it to be visible from the closure, e.g.

class A  
var = “initialized variable”  
class\_eval { define\_method(:talk) { puts var } }  
end

A.new.talk

#=\> initialized variable

Regards,

Sean

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 22, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/class-eval-used-inline-vs-within-class-definition/58510/3 "2006-04-22T15:22:03Z")

</div>

Hi –

On Sat, 22 Apr 2006, Pat M. wrote:

> > class A  
> > Sean
> 
> The point of this is to be able to access ‘var’ even though it’s  
> normally outside of the class’s scope. See the very first example,  
> which does this.

But look closely at it:

irb(main):001:0\> var = “initialized variable”  
=\> “initialized variable”  
irb(main):002:0\> class C; end  
=\> nil  
irb(main):003:0\> C.class\_eval { define\_method(“talk”) { puts var } }  
=\> #Proc:0x00355c30@:3(irb)  
irb(main):004:0\> C.new.talk  
initialized variable

The variable var is defined at the top level, and the call to  
class\_eval takes place at the top level. So var is in scope, and can  
be used inside the class\_eval block.

In your example, var is on one side of a class keyword, and the use of  
var is on the other. What you’ve done is like:

var = 1  
class C  
puts var  
end

which will give you the same error.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
Paper version coming in early May!

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 22, 2006, 3:34pm UTC](https://www.ruby-forum.com/t/class-eval-used-inline-vs-within-class-definition/58510/4 "2006-04-22T15:34:27Z")

</div>

On 4/22/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > A class definition introduces a new scope (similar to def) so for your  
> > > #=\> initialized variable
> 
> end  
> “Ruby for Rails” PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
> Paper version coming in early May!

Okay, that makes sense now. I think that’s what Sean was telling me  
as well…I just didn’t get it as quickly. Thanks for the  
explanation.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 22, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/class-eval-used-inline-vs-within-class-definition/58510/5 "2006-04-22T14:20:13Z")

</div>

On 4/22/06, Sean O’Halpin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> var = "initialized variable"
> 
> ```

The point of this is to be able to access ‘var’ even though it’s  
normally outside of the class’s scope. See the very first example,  
which does this.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 22, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/class-eval-used-inline-vs-within-class-definition/58510/6 "2006-04-22T16:39:12Z")

</div>

Hi –

On Sat, 22 Apr 2006, James Edward G. II wrote:

> > 
> 
> class:  
> I believe this to be an example of Dave B.'s new refactoring, Remove Unused  
> Scope.

To be fair to Sean, though, the question really had to do with  
clarifying how the scope of class\_eval worked – so removing it kind  
of defeats that particular purpose 🙂

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
Paper version coming in early May!

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 22, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/class-eval-used-inline-vs-within-class-definition/58510/7 "2006-04-22T16:33:07Z")

</div>

On Apr 22, 2006, at 6:52 AM, Sean O’Halpin wrote:

> ```
> var = "initialized variable"
> class_eval { define_method(:talk) { puts var } }
> 
> ```
> 
> end
> 
> A.new.talk
> 
> #=\> initialized variable

We might as well drop the class\_eval { … }, since we are already in  
the class:

> > class A  
> > var = “initialized variable”  
> > define\_method(:talk) { puts var }  
> > end  
> > =\> #Proc:0x0033307c@:3(irb)
> 
> > A.new.talk  
> > initialized variable  
> > =\> nil

I believe this to be an example of Dave B.'s new refactoring,  
Remove Unused Scope.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 22, 2006, 8:03pm UTC](https://www.ruby-forum.com/t/class-eval-used-inline-vs-within-class-definition/58510/8 "2006-04-22T20:03:57Z")

</div>

On 4/22/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> To be fair to Sean, though, the question really had to do with  
> clarifying how the scope of class\_eval worked – so removing it kind  
> of defeats that particular purpose 🙂
> 
> David

Thanks for leaping to my defence David but I think James is right.

Anyway, back to Pat’s question. I’ll try to be more explicit in future.

The main difference between

var = “initialized variable”  
class C;end  
C.class\_eval { define\_method(:test) { puts var} }

and

var = “initialized variable”  
class D  
define\_method(:test) { puts var } # will fail  
end

is that the closure passed to define\_method in D is  
within a new scope, i.e. the class definition, which means  
it does not have access to the local variables in the outer scope, i.e.  
var.

You can access variables local to the class definition like this:

class D  
var = “initialized variable”  
define\_method(:test) { puts var } # will work  
end

which is quite interesting in its own right.

But if you want access to the outer scope, then use the first method.  
Now you know why the code on page 350 of the RoR book was written that  
way!

Regards,

Sean

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 22, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/class-eval-used-inline-vs-within-class-definition/58510/9 "2006-04-22T19:54:29Z")

</div>

On 4/22/06, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> We might as well drop the class\_eval { … }, since we are already in  
> the class:
> 
> James Edward G. II

Quite right! The perils of cut and paste 😉

Sean
