Created
May 22, 2014 22:36
-
-
Save azhawkes/5b38135baa0e96a6025f to your computer and use it in GitHub Desktop.
Add a dynamic "commify" to Groovy's ArrayList
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
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