Skip to content

Instantly share code, notes, and snippets.

@pradhyu
Last active March 21, 2023 15:48
Show Gist options
  • Save pradhyu/e50f75fe0f1924cacb7ebba3a09cf556 to your computer and use it in GitHub Desktop.
Save pradhyu/e50f75fe0f1924cacb7ebba3a09cf556 to your computer and use it in GitHub Desktop.
Check if a variable is undefined in groovy
// checking bindings snippets
if (binding.hasVariable('superVariable')) {
// your code here
}
An easy solution to this is the following:
if (binding.variables.containsKey("bindingVar")) {
// do something
}
Or if you’d like to get a null value for an optional binding:
def optVar = binding.variables.get("bindingVar")
if (optVar) {
// do something
}
// Local vs global vars
def bindingVar (varName) {
def optVar = binding.variables.get(varName)
if (optVar) {
println(optVar)
//will only run for global var
}
optVar
}
def localVar="localVariable"
bindingVar("localVar")
globalVar="global variable"
bindingVar("globalVar")
/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment