for i in special_list
example = …
foobar = …
arr.push template % return_magical_hash_with_local_variables()
in Python there is a locals() function that returns a dictionary/hash of
local variables.
In Ruby I’ve found local_variables() but that does not provide the
values. It should for just the example above! Or perhaps I can be
enlightened by someone here?
The other, dirtier idea is something like this:
template = %q{
#{example}
...
#{foobar}
}
for i in special_list
example = …
foobar = …
arr.push eval("“template”")
I believe one should avoid calling eval as much as possible. I tried the
“Facets” package,
“String.interpolate{template}” calls eval, it just masks the fact.
This could be avoided if there were a function that return a dictionary
or hash of local variables.
I find this just dangerous and ugly, and I can’t think of a reason why
anyone would want to use this. At best it’s a demonstration how to
create security problems and confuse readers with unnecessary magic.
The practical solution for this is to simply save the values in a hash
– or even better, use a template engine.