Reputation: 347
A basic groovy question. In order to make my code robust, I need to make use of Eval.me(String) that groovy offers.
I have a problem though.It arises when I try to do string comparison.
def x='yay'
def y='yay'
def groovyString="'$x' == '$y'"
println Eval.me(groovyString);
This prints true.
But the below code just says 'yay' is not a variable definition,
def x='yay'
def y='yay'
def groovyString="$x == $y"
println Eval.me(groovyString);
I am aware as to why this error is coming :) Is there a way where i can sidestep this issue w/o actually needing to append ' '
for string comparisions??
P.S:I need it to work properly for more than just strings. Hence the apprehension to append ' '
Upvotes: 4
Views: 1955
Reputation: 171194
You should be able to do this with a Binding
and GroovyShell
If we make an instance of GroovyShell with a Binding that by default returns the name of the property when no property s found:
def shell = new GroovyShell( new Binding( [:].withDefault{ it } ) )
We can then evaluate your groovyString:
shell.evaluate( groovyString )
Which basically evaluates to the first form, as both yay
properties get resolved to the String yay
Upvotes: 2