how to reference an object from string in groovy? I have many objects – reviewSyd1, reviewSyd2..reviewSyd30 that i need to add to object – sydney. I have loop
for(i in 1..30){
sydney.addToReviews("reviewSyd$i")
}
This is not working. how to make it work?
Thank You
Updated to clarify(sorry if i wasn’t clear)
class Review{
String email
String Content
}
def rev1 = new Review(email:'one@homail.com',content:'i really like it')
def rev2 = new Review(email:'two@homail.com',content:'i really like it')
def rev3 = new Review(email:'three@homail.com',content:'i really like it')
def rev4 = new Review(email:'four@homail.com',content:'i really like it')
for(i in 1..4){
println rev$i.email
}
How to reference the object -revX where x is the iterator?
Not that dynamic is groovy. This not a valid groovy syntax.
Put those object, you want to access in later in your code,in a collection.
then you will have references to them: