示例代碼:閉包在Ruby中
def func(a, &closure)
return a if a
closure ||= lambda{ |words| puts "!!! " + words }
closure.call("1")
closure.call("2")
end
func(false){ |words| puts "??? " + words }
請解釋。如果刪除||
將如下永久顯示
closure ||= lambda{ |words| puts "!!! " + words }
:"!!! 1", "!!! 2"
我不明白這條線。爲什麼? 而且也解釋了這一點:
def func(a, &closure)
放哪兒&closure
。