0
隨着Hash.new
服用塊緩存值的快速方法,可以按如下方式創建緩存:爲哈希
cache = Hash.new do |hash, key|
hash[key] = expensive_calculation(key)
end
爲何如此複雜?
cache = Hash.cache do |key|
expensive_calculation(key)
end
這樣的版本會更快,因爲只有一個參數傳遞給塊。從我的經驗來看,這有很大的不同。