Created
August 25, 2016 14:38
-
-
Save anselm-helbig/8e8bd72aec623c7a0adbaa67f7a395e3 to your computer and use it in GitHub Desktop.
memoize macro for ruby
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
module Caching | |
def cache(method) | |
mod = Module.new do | |
define_method(method) do |*args, &block| | |
instance_variable_get("@#{method}") || | |
instance_variable_set("@#{method}", super(*args, &block)) | |
end | |
end | |
prepend mod | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment