Help with eval


K

Keith Carter

Am I missing something with eval? Why does this code not work?

eval("foo = 1")
puts foo

Gives:

C:\example\trunk>ruby script/runner lib/aggregate/keith.rb
C:/example/trunk/vendor/rails/railties/lib/commands/runner.rb:45:
undefined local variable or method `foo' for main:Obje
ct (NameError)
from
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `eval'
from
C:/noozler/trunk/vendor/rails/railties/lib/commands/runner.rb:45
from
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
from
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require'
from script/runner:3
 
Ad

Advertisements

D

Drew Olson

Keith said:
Am I missing something with eval? Why does this code not work?

Maybe it's a scoping issue with where you're running your code...works
fine for me in irb:
=> 1
 
K

Keith Carter

Hm. You're right, that did work. How come the second bit below doesn't
work?

Loading development environment (Rails 2.0.2)NameError: undefined local variable or method `bar' for
 
Y

yermej

Hm. You're right, that did work. How come the second bit below doesn't
work?

Loading development environment (Rails 2.0.2)>> eval("foo = 1")
=> 1

NameError: undefined local variable or method `bar' for
#<Object:0x27bf9ec>
from (irb):5

For even more fun, evaluate bar once you've gotten the NameError.
=> 1

Sorry, but I don't know why, only that it seems odd.
 
K

Keith Carter

yermej said:
Sorry, but I don't know why, only that it seems odd.

Wow. That is weird. I think evals are scoped as blocks. I'm going to
play with this for a bit.
 
A

ara.t.howard

Wow. That is weird. I think evals are scoped as blocks. I'm going to
play with this for a bit.


variables created in eval can only been seen from eval. when you are
irb you are already inside eval. there are some exceptions - search
the archives.

if you post what you are trying to accomplish perhaps someone can
help - no pattern that relies on eval creating vars in the local
scope is going to pan out too well without a little dsl or some
hackery - eval just won't suffice.

regards.

a @ http://codeforpeople.com/
 
Ad

Advertisements

K

Keith Carter

ara.t.howard said:
if you post what you are trying to accomplish perhaps someone can
help - no pattern that relies on eval creating vars in the local
scope is going to pan out too well without a little dsl or some
hackery - eval just won't suffice.

I'm not trying to accomplish anything too fancy, and I was able to skirt
around the problem by declaring my variables outside of the eval block.

But now, my interest is piqued. What is dsl? What if I wanted to declare
a variable with a dynamic name using an eval block? (And I wanted to use
it outside of the eval block)
 
C

Chirantan

variables created in eval can only been seen from eval. when you are
irb you are already inside eval. there are some exceptions - search
the archives.

if you post what you are trying to accomplish perhaps someone can
help - no pattern that relies on eval creating vars in the local
scope is going to pan out too well without a little dsl or some
hackery - eval just won't suffice.

regards.

a @http://codeforpeople.com/

begin
eval("bar=1")
puts bar
end

Gives me and error too. but if I do

bar = 0
begin
eval("bar=1")
puts bar
end

It works! I am a ruby rookie so I dont know why this is so.
 
Ad

Advertisements

K

Keith Carter

Chirantan said:
It works! I am a ruby rookie so I dont know why this is so.

In the first example, you are defining the variable inside of the block.

In the second example, you've defined the variable outside of the block.

evals are scoped as blocks, and so variables created in them are not
available outside of the block.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top