# Where does instance\_eval \_magically\_ set the variable to?

**URL:** <https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680>\
**Category:** ruby-talk\
**Created:** [21 February 2008 19:44 UTC](https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680 "2008-02-21T19:44:58Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Chen](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Andrew\_Chen](https://rubytalk.org/u/Andrew_Chen)\
**Post date:** [21 February 2008 19:44 UTC](https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680/1 "2008-02-21T19:44:58Z")

</div>

Hi,

I use the instance\_eval to merge template file and parameter files.

\>cat param.txt  
name="John"  
\>cat tmpl.txt  
my name is: \<%= name %\>

\>cat template.rb  
#!/usr/bin/env ruby  
require 'erb'

instance\_eval(File.read('param.txt'))  
puts ERB.new(File.read("tmpl.txt")).result(binding)

puts name

\>template.rb  
my name is: John  
template.rb:7: undefined method `name' for main:Object (NoMethodError)

Where is the variable "name" set to? How to access it?  
It must be somewhere, since it is used in the tmpl.txt file.

Is there any "magic" namespace in Ruby?

Thanks  
~Andrew Chen

---

<div class="post-metadata">

**Author:** ![Jari\_Williamsson](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@Jari\_Williamsson](https://rubytalk.org/u/Jari_Williamsson)\
**Post date:** [21 February 2008 20:01 UTC](https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680/2 "2008-02-21T20:01:25Z")

</div>

Andrew Chen wrote:

> instance\_eval(File.read('param.txt'))

How about:  
load 'param.txt'

Best regards,

Jari Williamsson

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [21 February 2008 20:04 UTC](https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680/3 "2008-02-21T20:04:16Z")

</div>

> Hi,
> 
> I use the instance\_eval to merge template file and parameter files.
> 
> &nbsp;&nbsp;\>cat param.txt  
> name="John"  
> &nbsp;&nbsp;\>cat tmpl.txt  
> my name is: \<%= name %\>
> 
> &nbsp;&nbsp;\>cat template.rb  
> #!/usr/bin/env ruby  
> require 'erb'

name = nil

> instance\_eval(File.read('param.txt'))  
> puts ERB.new(File.read("tmpl.txt")).result(binding)
> 
> puts name
> 
> &nbsp;&nbsp;\>template.rb  
> my name is: John  
> template.rb:7: undefined method `name' for main:Object (NoMethodError)
> 
> Where is the variable "name" set to? How to access it?  
> It must be somewhere, since it is used in the tmpl.txt file.
> 
> Is there any "magic" namespace in Ruby?

It's just a scope issue. If you use name = nil like I did above you  
should be okay. I wonder, though, why you would want to  
#instance\_eval instead of using #load.

Also, some rubyists advocate using the block method of File.open as  
well. I'm not sure if File.read closes the file automatically.

hth,  
Todd

> **···**
>
> On Thu, Feb 21, 2008 at 1:44 PM, Andrew Chen \<hangfei@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Andrew\_Chen](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Andrew\_Chen](https://rubytalk.org/u/Andrew_Chen)\
**Post date:** [21 February 2008 20:10 UTC](https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680/4 "2008-02-21T20:10:15Z")

</div>

Jeri,

> How about:  
> load 'param.txt'

Load doesn't get the variable into the current namespace. Maybe I  
used it wrong?

\>cat param.txt  
name='John'  
\>cat load.rb  
#!/usr/bin/env ruby

load 'param.txt'

puts name  
\>./load.rb  
./load.rb:5: undefined local variable or method `name' for main:Object  
(NameError)

> **···**
>
> >

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [21 February 2008 20:22 UTC](https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680/5 "2008-02-21T20:22:08Z")

</div>

On Thu, Feb 21, 2008 at 2:04 PM, Todd Benson

> I wonder, though, why you would want to  
> #instance\_eval instead of using #load.

If those are just parameters, you might consider using the YAML  
library. Just a thought...

Todd

---

<div class="post-metadata">

**Author:** ![Andrew\_Chen](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Andrew\_Chen](https://rubytalk.org/u/Andrew_Chen)\
**Post date:** [21 February 2008 20:44 UTC](https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680/6 "2008-02-21T20:44:56Z")

</div>

Todd,  
Yes, load more proper here than instance\_eval.  
Also, I will use File.open block instead of File.read.

However, this is still a mystery:  
In the original code, where did the variable "name" go? How to access  
that?

When I use load 'param.txt', how to access variables loaded from the  
param.txt file?

\>cat param.txt  
Name='John'  
Name='John'  
last="smith"  
&nbsp;&nbsp;\>./load.rb  
./param.txt:2: warning: already initialized constant Name  
John  
./load.rb:6: undefined local variable or method `last' for main:Object  
(NameError)  
\>

Thanks  
~Andrew Chen

---

<div class="post-metadata">

**Author:** ![Andrew\_Chen](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Andrew\_Chen](https://rubytalk.org/u/Andrew_Chen)\
**Post date:** [21 February 2008 21:14 UTC](https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680/7 "2008-02-21T21:14:56Z")

</div>

Todd,  
There are some subtle difference between load and instance\_eval.  
In this case, instance\_eval works, the load doesn't.

I still have no idea where the instance\_eval put the variables at.

\>cat param.txt  
name="John"  
\>cat tmpl.txt  
my name is: \<%= name %\>  
\>cat erb\_instance\_eval.rb  
#!/usr/bin/env ruby

require 'erb'

instance\_eval(File.read("param.txt"))  
#load "param.txt"  
puts ERB.new(File.read("tmpl.txt")).result(binding)  
\>./erb\_instance\_eval.rb  
my name is: John

\>cat erb\_load.rb  
#!/usr/bin/env ruby

require 'erb'

#instance\_eval(File.read("param.txt"))  
load "param.txt"  
puts ERB.new(File.read("tmpl.txt")).result(binding)  
\>./erb\_load.rb  
(erb):1: undefined local variable or method `name' for main:Object  
(NameError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from ./erb\_load.rb:7  
\>

Also, the load\_yaml won't work, because I need to setup variables  
dynamically.

Thanks  
~Andrew Chen

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [21 February 2008 20:27 UTC](https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680/8 "2008-02-21T20:27:59Z")

</div>

Nope, you didn't. I'm sure there's a reason for that; probably to  
prevent accidental corruption of namespace. If you must use load,  
you'll have to build methods in the loaded file instead of  
variables...

def name; 'John'; end

...which may or may not work for you.

I suggest going the YAML route. I suppose there are other ways to  
serialize data, too.

Todd

> **···**
>
> On Thu, Feb 21, 2008 at 2:10 PM, Andrew Chen \<hangfei@gmail.com\> wrote:
> 
> > Jeri,
> > 
> > \> How about:  
> > \> load 'param.txt'  
> > \>
> > 
> > Load doesn't get the variable into the current namespace. Maybe I  
> > used it wrong?

---

<div class="post-metadata">

**Author:** ![Jari\_Williamsson](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@Jari\_Williamsson](https://rubytalk.org/u/Jari_Williamsson)\
**Post date:** [21 February 2008 21:48 UTC](https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680/9 "2008-02-21T21:48:52Z")

</div>

Strange. The REALLY interesting stuff starts to happen when you encapsulate the code in a module. (name becomes the name of the module)

Best regards,

Jari Williamsson

Andrew Chen wrote:

> **···**
>
> > Todd,  
> > There are some subtle difference between load and instance\_eval.  
> > In this case, instance\_eval works, the load doesn't.
> > 
> > I still have no idea where the instance\_eval put the variables at.
> > 
> > \>cat param.txt  
> > name="John"  
> > \>cat tmpl.txt  
> > my name is: \<%= name %\>  
> > \>cat erb\_instance\_eval.rb  
> > #!/usr/bin/env ruby
> > 
> > require 'erb'
> > 
> > instance\_eval(File.read("param.txt"))  
> > #load "param.txt"  
> > puts ERB.new(File.read("tmpl.txt")).result(binding)  
> > \>./erb\_instance\_eval.rb  
> > my name is: John
> > 
> > \>cat erb\_load.rb  
> > #!/usr/bin/env ruby
> > 
> > require 'erb'
> > 
> > #instance\_eval(File.read("param.txt"))  
> > load "param.txt"  
> > puts ERB.new(File.read("tmpl.txt")).result(binding)  
> > \>./erb\_load.rb  
> > (erb):1: undefined local variable or method `name' for main:Object  
> > (NameError)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from ./erb\_load.rb:7  
> > \>
> > 
> > Also, the load\_yaml won't work, because I need to setup variables  
> > dynamically.
> > 
> > Thanks  
> > ~Andrew Chen

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [21 February 2008 21:53 UTC](https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680/10 "2008-02-21T21:53:22Z")

</div>

It's signed up for garbage collection when you exit the #instance\_eval  
or #load method (I think). In other words, it has a temporary  
existence. There's nothing holding on to it (referencing it).

Todd

> **···**
>
> On Thu, Feb 21, 2008 at 2:44 PM, Andrew Chen \<hangfei@gmail.com\> wrote:
> 
> > Todd,  
> > Yes, load more proper here than instance\_eval.  
> > Also, I will use File.open block instead of File.read.
> > 
> > However, this is still a mystery:  
> > In the original code, where did the variable "name" go? How to access  
> > that?

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [22 February 2008 11:32 UTC](https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680/11 "2008-02-22T11:32:54Z")

</div>

You want to do variable assignment via a file, right? Is it not  
possible to do this?

$ cat a.yml  
name: John  
$ cat a.rb  
h = YAML.load\_file('a.yml')  
puts h['name']

Todd

> **···**
>
> On Thu, Feb 21, 2008 at 3:14 PM, Andrew Chen \<hangfei@gmail.com\> wrote:
> 
> > Also, the load\_yaml won't work, because I need to setup variables  
> > dynamically.

---

<div class="post-metadata">

**Author:** ![Andrew\_Chen](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Andrew\_Chen](https://rubytalk.org/u/Andrew_Chen)\
**Post date:** [21 February 2008 20:49 UTC](https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680/12 "2008-02-21T20:49:55Z")

</div>

Todd,  
"I suggest going the YAML route. I suppose there are other ways to  
serialize data, too."

I thought about yaml.

However, I want to make a parameter file for easy template. Maybe I  
can do a pre-transformation from ruby data to yaml, then from yaml to  
data.

My goal is to read data from a parameter file (ruby data format) into  
memory, without @, $, Uppercase\_name.

Thanks  
~Andrew Chen

---

<div class="post-metadata">

**Author:** ![Arlen\_Christian\_Mar1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Arlen\_Christian\_Mar1](https://rubytalk.org/u/Arlen_Christian_Mar1)\
**Post date:** [21 February 2008 23:05 UTC](https://rubytalk.org/t/where-does-instance-eval-magically-set-the-variable-to/44680/13 "2008-02-21T23:05:38Z")

</div>

Hi all,

The reason is it's a local variable inside a block. Have a look at this, for  
example:

> > 1.upto(5) {|i| j = i \*\* 2; puts j}

1  
4  
9  
16  
25  
=\> 1

> > j

NameError: undefined local variable or method `j' for main:Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):2

> >

j is undefined after the block's execution. If we let j be defined before  
the block, however, ...

> > j = "anything at all, really"

=\> "anything at all, really"

> > 1.upto(5) {|i| j = i \*\* 2; puts j}

1  
4  
9  
16  
25  
=\> 1

> > j

=\> 25

Since j is already defined, Ruby uses that instead. Confusing, but could be  
useful.

Arlen
