-
-
Save rashkur/a5e2b56a6d0990f294cb08865176270c to your computer and use it in GitHub Desktop.
Check if a variable is undefined in groovy
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
// 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