Last active
May 25, 2017 07:21
-
-
Save draganHR/5fc867c373960d8fc5380eda6c472432 to your computer and use it in GitHub Desktop.
Groovy examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A { | |
def call (c) { | |
c.call() | |
} | |
def foobar (c) { | |
call(c) | |
} | |
} | |
def a = new A() | |
a { | |
println "hello..." | |
} | |
a.foobar { | |
println "world..." | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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