Last active
March 21, 2023 15:48
-
-
Save pradhyu/e50f75fe0f1924cacb7ebba3a09cf556 to your computer and use it in GitHub Desktop.
Check if a variable is undefined in groovy
This file contains 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
// 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