Runtime evaluation: Difference between revisions

Line 87:
 
====Binding variables====
The '''BindingGroovyShell''' classuses cana be'''Binding''' usedobject to pass variable values to a script. This is the only way to pass variables if the script comes from a '''File''' or '''InputStream''', but even if the script is a string '''Binding''' avoids the whole nested quoting issue caused by the ''ad hoc'' use of '''GString''' quoting issue.
<lang groovy>def context = new Binding()
context.startYear = 2008
Line 97:
''')</lang>
 
We may instantiate '''Binding''' can be instantiated with the variables as named parameters, allowing a more terse syntax:
<lang groovy>def years4 = new GroovyShell( new Binding(startYear: 2008, endYear: 2121) ).evaluate('''
(startYear..endYear).findAll {
Line 106:
println years4</lang>
 
We may also access the '''Binding''' can also be usedobject ''after'' script evaluation to extract the valuevalues of any global variable that wasvariables set during the evaluation:
<lang groovy>def binding = new Binding(startYear: 2008, endYear: 2121)
new GroovyShell( binding ).evaluate('''
Anonymous user