Skip to content

Instantly share code, notes, and snippets.

@toby55kij
Created October 10, 2010 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toby55kij/619088 to your computer and use it in GitHub Desktop.
Save toby55kij/619088 to your computer and use it in GitHub Desktop.
// g100pon #65 何でも使えるぜGroovyなswitch-case
def check(value) {
switch (value) {
case 0: println 'zero';break // 普通
case 1..9: println '1to9';break // 範囲指定
case [10, 12, 14]: println '10or12or14';break // リストで指定
case {it instanceof Integer && it % 7 == 0}: println '7,14,21,...';break // クロージャ
case ~/gr.*/: println 'gr*';break // 正規表現
case 'foo': println 'FOO';break // 文字列
case String: println 'String';break // クラス
default: println 'Other'
}
}
check 0 // zero
check 6 // 1to9
check 12 // 10or12or14
check 49 // 7,14,21,...
check 'foo' // FOO
check 'bar' // String
check 'groovy' // gr*
check {name:'this'} // Other
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment