Created
October 8, 2010 13:59
-
-
Save fumokmm/616841 to your computer and use it in GitHub Desktop.
This file contains 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
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