ActiveSupport::Memoizable(带有少量增强功能)
require 'memoist' class Person extend Memoist
def social_security puts "execute!" decrypt_social_security end memoize :social_security end
person = Person.new
person.social_security
person.social_security
从而, person.social_security 只会被计算一次。 每个 memoized 函数 (最初不接受任何参数) 都有一个 `(reload)` 参数, 你可以传递给它来绕过和重置 memoization:ruby
def some_method
Time.now
end
memoize :some_method
执行 some_method 将会被 memoized, 但执行 some_method(true) 则每次都会重新 memoize。 你甚至可以 memoize 接受参数的方法。ruby
class Person
def taxes_due(income)
income * 0.40
end
memoize :taxes_due
end
# 相关链接暂无开放 Issues,或尚未同步最近议题。