Skip to content

Instantly share code, notes, and snippets.

@paulk-asert
Created May 28, 2015 11:14
Show Gist options
  • Save paulk-asert/5d0ab5c91db3b32cfb22 to your computer and use it in GitHub Desktop.
Save paulk-asert/5d0ab5c91db3b32cfb22 to your computer and use it in GitHub Desktop.
@groovy.transform.TypeChecked
def myMethod() {
def s = 'The 3 quick\nbrown 4 fox'
def expected = 'The _ QUICK|brown _ FOX|'
def result = ''
new StringReader(s).splitEachLine(~/\d/){ result += "${it[0]}_${it[1].toUpperCase()}|" }
assert result == expected; result = ''
new StringReader(s).splitEachLine(~/\d/){ parts -> result += "${parts[0]}_${parts[1].toUpperCase()}|" }
assert result == expected; result = ''
new StringReader(s).splitEachLine(~/\d/){ List parts -> result += "${parts[0]}_${parts[1].toUpperCase()}|" }
assert result == expected; result = ''
new StringReader(s).splitEachLine(~/\d/){ p1, p2 -> result += "${p1}_${p2.toUpperCase()}|" }
assert result == expected; result = ''
new StringReader(s).splitEachLine(~/\d/){ String p1, String p2 -> result += "${p1}_${p2.toUpperCase()}|" }
assert result == expected; result = ''
new StringReader(s).splitEachLine(~/\d/){ String p1 -> result += "${p1.toUpperCase()}|" }
assert result == 'THE |BROWN |'; result = ''
}
myMethod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment