[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Help with eval

Keith Carter

1/23/2008 8:57:00 PM

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
--
Posted via http://www.ruby-....

8 Answers

Drew Olson

1/23/2008 9:26:00 PM

0

Keith Carter wrote:
> 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:

>> eval("foo = 1")
=> 1
>> foo
=> 1


--
Posted via http://www.ruby-....

Keith Carter

1/23/2008 10:13:00 PM

0

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
>> foo
=> 1
>> begin
?> eval("bar=1")
>> bar
>> end
NameError: undefined local variable or method `bar' for
#<Object:0x27bf9ec>
from (irb):5
>>
--
Posted via http://www.ruby-....

yermej

1/23/2008 10:58:00 PM

0

On Jan 23, 4:13 pm, Keith Carter <kmarplecar...@yahoo.com> wrote:
> 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
> >> foo
> => 1
> >> begin
> ?> eval("bar=1")
> >> bar
> >> end
>
> 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.

>> bar
=> 1

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

Keith Carter

1/23/2008 11:07:00 PM

0

yermej wrote:
> 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.

--
Posted via http://www.ruby-....

ara.t.howard

1/24/2008 12:51:00 AM

0


On Jan 23, 2008, at 4:06 PM, Keith Carter wrote:

> 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://codeforp...
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama



Keith Carter

1/24/2008 3:39:00 AM

0

ara.t.howard wrote:
>
> 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)
--
Posted via http://www.ruby-....

Chirantan

1/24/2008 5:02:00 AM

0

On Jan 24, 5:50 am, "ara.t.howard" <ara.t.how...@gmail.com> wrote:
> On Jan 23, 2008, at 4:06 PM, Keith Carter wrote:
>
> > 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://codeforp...
> --
> share your knowledge. it's a way to achieve immortality.
> h.h. the 14th dalai lama

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.

Keith Carter

1/24/2008 5:17:00 AM

0

Chirantan wrote:
> 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.

--
Posted via http://www.ruby-....