On 12/16/06, Steve Midgley <public / misuse.org> wrote:
> Hi Chris,
>
> def test
>    static = "static"
>    dyno = "_dynamic1"
>    data = "store me"
>    eval(static+dyno+"='"+data+"'")
>    # call it via hard code
>    #call it dynamically
>    eval("puts "+static+dyno)
> end
>
> test
>
> I think this gets you where you want to be. For some reason, calling
> "static_dynamic1" hard coded wasn't working for me but if you need to
> go both ways, I'm sure it's possible.

I believe this is because local variable-ness is determined
statically, so it won't recognize 'a' as a local unless it was a local
before the eval.  Hence:

g@crash:~$ ruby -e "eval('a = 1'); p a"
-e:1: undefined local variable or method `a' for main:Object (NameError)
g@crash:~$ ruby -e "a = nil; eval('a = 1'); p a"
1

evalling the 'a', OTOH, causes local variable-ness to be checked when
the eval is run, so:

g@crash:~$ ruby -e "eval('a = 1'); p a"
-e:1: undefined local variable or method `a' for main:Object (NameError)
g@crash:~$ ruby -e "eval('a = 1'); p eval('a')"
1

I know you were just answering the OP's question.  I tend to think all
this evalling business should really be avoided to begin with, though.
 ;-)