Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add a dynamic "commify" to Groovy's ArrayList
class BootStrap {
def init = { servletContext ->
ArrayList.metaClass.commify = {
StringBuilder output = new StringBuilder()
for (int i = 0; i < delegate.size(); i++) {
if (i == 0) {
output.append(delegate[i])
} else if (i == delegate.size() - 1) {
output.append(" and ${delegate[i]}")
} else {
output.append(", ${delegate[i]}")
}
}
return output.toString()
}
}
def destroy = {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment