I’m sure I’m over simplifying this, but I want to resolve a variable with another manufactured variable. This is a generic sample of my syntax.
def sMonth = 'Jan'
def aVar = 'Month'
def bVar = 's' + aVar
println("${bVar}")
Ultimately, I want bVar to resolve to ‘Jan’, but understandably it’s resolving to ‘sMonth’. How can I get Groovy to resolve to the variable I created?
I’ve tried many variations of the println, but they all keep resolving to sMonth.
Richard Schaefer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
In a script like this, there is no access to local bindings. The only
chance here is to “promote” the bindings to fields and the access those
by their name. E.g.
@groovy.transform.Field
def sMonth = 'Jan'
def aVar = 'Month'
def bVar = 's' + aVar
println(this."$bVar")
println(getProperty(bVar))