Skip to content

Instantly share code, notes, and snippets.

@draganHR
Last active May 25, 2017 07:21
Show Gist options
  • Save draganHR/5fc867c373960d8fc5380eda6c472432 to your computer and use it in GitHub Desktop.
Save draganHR/5fc867c373960d8fc5380eda6c472432 to your computer and use it in GitHub Desktop.
Groovy examples
class A {
def call (c) {
c.call()
}
def foobar (c) {
call(c)
}
}
def a = new A()
a {
println "hello..."
}
a.foobar {
println "world..."
}
def ifstash(opt, name, includes) {
println "ifstash ${opt} ${name} ${includes}"
dir(name) {
sh 'rm -rf ./*'
}
if (opt) {
stash name: name, includes: includes
} else {
dir(name) {
try {
unstash name
} catch (error) {
println "${error}"
}
}
}
}
println "GroovySystem.version: ${GroovySystem.version}"
def getClosure (context, Closure c) {
return {
println "Before closure"
c(context);
println "After closure"
}();
}
getClosure ("Hello World") {context ->
println "Doing the work; Context: ${context}";
}
println "GroovySystem.version: ${GroovySystem.version}"
def withContext = {Object context, Closure c ->
println "Start; Context: ${context}"
c();
try {
println "Before closure"
c(context);
println "After closure"
} catch (e) {
println "In catch: ${e}"
} finally {
println "In finally"
}
println "End; Context: ${context}"
}
withContext ("Hello World") { Object context ->
println "Doing the work; Context: ${context}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment