0
随着Hash.new
服用块缓存值的快速方法,可以按如下方式创建缓存:为哈希
cache = Hash.new do |hash, key|
hash[key] = expensive_calculation(key)
end
为何如此复杂?
cache = Hash.cache do |key|
expensive_calculation(key)
end
这样的版本会更快,因为只有一个参数传递给块。从我的经验来看,这有很大的不同。