Groovy web console

subscribe to the feed Subscribe
to this
VarArgs split up (via #groovywebconsole)
tweet this snippet Tweet

VarArgs split up

Published 11 months ago by Adz
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def singleArg = { String myarg ->
println "Single arg: $myarg"

def twoArgs = { String myarg1, String myarg2 ->
println "Two args: $myarg1 $myarg2"

def multipleArgs = { String myarg1, String myarg2, String myarg3, String myarg4 ->
println "Multiple args: $myarg1 $myarg2 $myarg3 $myarg4"

void invokeAClosure(Closure c, Object... params) {
  c(*params) // * will 'split up' the arguments and pass them into the closure.

String script = """
  singleArg 'field'
  twoArgs 'field', 'field2'
  multipleArgs 'field', 'field2', 'field3', 'field4'


Binding b = new Binding([singleArg: singleArg, twoArgs: twoArgs, multipleArgs: multipleArgs])
GroovyShell shell = new GroovyShell(b)