# Why set hash with 'eval' doesn't work?

**URL:** <https://www.ruby-forum.com/t/why-set-hash-with-eval-doesnt-work/238420>\
**Category:** Ruby\
**Created:** [January 9, 2014, 10:28am UTC](https://www.ruby-forum.com/t/why-set-hash-with-eval-doesnt-work/238420 "2014-01-09T10:28:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [January 9, 2014, 10:28am UTC](https://www.ruby-forum.com/t/why-set-hash-with-eval-doesnt-work/238420/1 "2014-01-09T10:28:27Z")

</div>

=========================

1. cat list  
=========================  
lic\_servers = {  
‘10071@server1’ =\> 10,  
‘10072@server2’ =\> 10  
}  
=========================
2. cat test.rb  
=========================  
#!/usr/bin/env ruby

csh\_file = ‘list’  
sign = `cat #{csh_file}`  
a = eval(sign)  
puts a

puts lic\_servers

# ========================= 3. run test.rb

{“10071@server1”=\>10, “10072@server2”=\>10}  
test.rb:8:in `<main>': undefined local variable or method`lic\_servers’  
for main:Object (NameError)

Why it report ‘lic\_servers’ undefined? I hope lic\_servers set by ‘eval’,  
but seems doesn’t work, why?

- The object set lic\_servers in file ‘list’ is for the servers list  
always change, I want split it from main ruby script.

---

<div class="post-metadata">

**Author:** ![previn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@previn](https://www.ruby-forum.com/u/previn)\
**Post date:** [January 9, 2014, 10:41am UTC](https://www.ruby-forum.com/t/why-set-hash-with-eval-doesnt-work/238420/2 "2014-01-09T10:41:43Z")

</div>

See

> <https://stackoverflow.com/questions/17172187/variable-assignment-using-eval>

Eval creates the variable in his own scope. If you want is to be  
available you should use an instance variable

@lic\_servers = {…}

and in test.rb

puts @lic\_servers

Lars

---

<div class="post-metadata">

**Author:** ![previn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@previn](https://www.ruby-forum.com/u/previn)\
**Post date:** [January 9, 2014, 11:21am UTC](https://www.ruby-forum.com/t/why-set-hash-with-eval-doesnt-work/238420/3 "2014-01-09T11:21:30Z")

</div>

Another way is to define the local var before the eval so that it will  
be accessible after it.

# ========================= 2. cat test.rb

#!/usr/bin/env ruby

lic\_servers = Hash.new # \<-- Define it before  
csh\_file = ‘list’  
sign = `cat #{csh_file}`  
a = eval(sign)  
puts a

puts lic\_servers

=====

Another example.

10.times do |x|  
my\_local = x  
end

if defined? my\_local  
puts “my\_local = #{my\_local}”  
else  
puts “my\_local is not defined”  
end

# Second round with local var being defined outside the block

my\_local = 0

10.times do |x|  
my\_local = x  
end

if defined? my\_local  
puts “my\_local = #{my\_local}”  
else  
puts “my\_local is not defined”  
end

# This outputs

my\_local is not defined  
my\_local = 9

Abinoam Jr.

---

<div class="post-metadata">

**Author:** ![previn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@previn](https://www.ruby-forum.com/u/previn)\
**Post date:** [January 10, 2014, 2:37am UTC](https://www.ruby-forum.com/t/why-set-hash-with-eval-doesnt-work/238420/4 "2014-01-10T02:37:21Z")

</div>

Lars Vonk wrote in post #1132612:

> See  
> [http://stackoverflow.com/questions/17172187/variable-assignment-using-eval](http://stackoverflow.com/questions/17172187/variable-assignment-using-eval)
> 
> Eval creates the variable in his own scope. If you want is to be  
> available you should use an instance variable
> 
> @lic\_servers = {…}
> 
> and in test.rb
> 
> puts @lic\_servers
> 
> Lars

> Eval creates the variable in his own scope  
> Understand now, many thanks.

> If you want is to be  
> available you should use an instance variable
> 
> @lic\_servers = {…}

May I ask one more question? As I know, instance variable should belongs  
to specific class, so here @lic\_servers belongs to which class, Main  
class?

---

<div class="post-metadata">

**Author:** ![previn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@previn](https://www.ruby-forum.com/u/previn)\
**Post date:** [January 14, 2014, 9:34am UTC](https://www.ruby-forum.com/t/why-set-hash-with-eval-doesnt-work/238420/5 "2014-01-14T09:34:29Z")

</div>

Dear Alex,

Thank you so much for your guide and help, I understand now.

---

<div class="post-metadata">

**Author:** ![previn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@previn](https://www.ruby-forum.com/u/previn)\
**Post date:** [January 10, 2014, 3:40am UTC](https://www.ruby-forum.com/t/why-set-hash-with-eval-doesnt-work/238420/6 "2014-01-10T03:40:10Z")

</div>

On Thu, Jan 9, 2014 at 5:37 PM, Previn L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> May I ask one more question? As I know, instance variable should belongs  
> to specific class, so here @lic\_servers belongs to which class, Main  
> class?

This is a great question. Actually, an instance variable belongs to an  
_instance_, not a class. You can tell which instance by looking at the  
self variable. At the top level self points to a magic object called  
main (not Main) so that’s where @lic\_servers lives.

Type “self” and “self.class” at irb to see for yourself.

If it’s still unclear, maybe my slides at  
[Objects - Code Like This](http://codelikethis.com/lessons/ruby_objects/objects#state) will  
clarify.
