tintin
tintin

Reputation: 5867

String interpolation not working in closure

I am looking at GString $ keyword and want to print the range.

('A'..'Z').each {item ->
    print '$item'
}

I was expecting the range A..Z to be printed, but its printing $item$item.... What am I missing here?

Upvotes: 1

Views: 567

Answers (1)

Paweł Piecyk
Paweł Piecyk

Reputation: 2789

Single quoted strings are java.lang.String and interpolation doesn't work here. You need to change it to GString, use double quotes:

('A'..'Z').each {item ->
    print "$item"
}

It's explained pretty well in Groovy's documentation about strings.

Upvotes: 3

Related Questions