How to create a closure from a string?
Like I have a string def formula = "{it * it}"
Now formula is a string and I can't do formula()
how do I define the code block in the string variable formula as a closure?
How to create a closure from a string?
Like I have a string def formula = "{it * it}"
Now formula is a string and I can't do formula()
how do I define the code block in the string variable formula as a closure?
You can do:
def formula = Eval.me( "{ x -> x * x}" )
formula( 3 )
Obviously the usual warnings about the safety of evaluating strings entered by users apply
You might also be interested in a nearly identical question that was asked yesterday