Skip to content

Instantly share code, notes, and snippets.

@fumokmm
Created October 8, 2010 13:59
Show Gist options
  • Save fumokmm/616841 to your computer and use it in GitHub Desktop.
Save fumokmm/616841 to your computer and use it in GitHub Desktop.
def test(Closure clos) {
def localVals = new Object() {
def map = [:]
def that
}
clos.delegate = localVals // 参照先を一時変数とする
def arg1 = clos.getResolveStrategy() // デフォルトではowner, delegateの順でメソッドやプロパティの存在を確認
localVals.that = 'This is that!' // thatで参照できるようにしてみる。
clos.call(arg1)
localVals.map.each { key, value ->
if (value instanceof Closure)
println "[$key = ${value()}]"
else
println "[$key = $value]"
}
}
test {
println it // => 一つ目の引数
println this // => このクラス
println owner // => このクロージャの保有者
println delegate // => このクロージャの移譲先
println that // thatで指定された文字列
map.count = 1 // mapも参照できる
map.clos = { that * 3 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment