I would like to use Eval.me to expand a string which contains a variable name.
def var = '1234'
// This works
eval_str = "println $var"
Eval.me(eval_str)
// This does not: No such property: $var
eval_str = 'println $' + "var" // I hoped later replace "var" with a parameter
Eval.me(eval_str)
So as I see Eval.me cannot do this. Then what is the sense in this Eval.me at all if it does not do anything more than the expression itself? Only for math expressions?
Is there any workaround, with Reflections or what ever to enforce it to expand the local def-variable (not Binding and not global one) from a string?
UPDATE: Added a minimal program fragment to explain, why I need variable expansion and what does not work in my case:
def func(def value_for_local_var, def closure_as_param) {
def local_var = value_for_local_var
closure_as_param()
}
// main
func('1234',
{
// println(local_var) // <- fails: "local_var" is visible only for func()
// Eval.x(local_var, 'println "${x}') // <- again I need the value of "local_var" before
// If I could something like this ...:
Eval.me('println "${local_var}"') // but Eval.me is not so advanced as I thought
}
)
Eval.me is not so advanced as I thought- you are just cooking it incorrectly :)