Jump to content

Runtime evaluation: Difference between revisions

(omit autohotkey)
Line 134:
'''Binding''' variables<br>
'''GroovyShell''' uses a '''Binding''' object 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 nested quoting issue caused by the ''ad hoc'' use of '''GString'''.
<lang groovy>def context = new Binding()
context.startYear = 2008
context.endYear = 2121
Line 144:
 
We may instantiate '''Binding''' 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 {
Date.parse("yyyy-MM-dd", "${it}-12-25").format("EEE") == "Sun"
Line 153:
 
We may also access the '''Binding''' object ''after'' script evaluation to extract values of any global variables set during the evaluation:
<lang groovy>def binding = new Binding(startYear: 2008, endYear: 2121)
new GroovyShell( binding ).evaluate('''
yearList = (startYear..endYear).findAll {
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.