2008/1/18, Jari W. [email protected]:
understand your sample code correctly. And calling the
create_some_variables() multiple times would create multiple stacked blocks.
But giving it a bit more thought, I think I should redesign the code a
bit and let the method instead dynamically create a class where all the
created variables are stored and then returned an instance of that class.
OpenStruct and Hash come to mind. One of those is usually far better
than the hack you attempted initially. Btw, there is another issue
with this: local variables created dynamically in a scope cannot be
used directly (i.e. the same way as if they were defined in the scope
$ ruby -e ‘def f() eval(“x=1”, binding); p x end; f’
f': undefined local variable or methodx’ for main:Object
$ ruby -e ‘def f() eval(“x=1”, binding); p(eval(“x”, binding)) end; f’
So you need to use some name passing mechanism anyway. And if you do
that you can use a Hash immediately which is much easier and clearer.