Skip to content

Instantly share code, notes, and snippets.

@anselm-helbig
Created August 25, 2016 14:38
Show Gist options
  • Save anselm-helbig/8e8bd72aec623c7a0adbaa67f7a395e3 to your computer and use it in GitHub Desktop.
Save anselm-helbig/8e8bd72aec623c7a0adbaa67f7a395e3 to your computer and use it in GitHub Desktop.
memoize macro for ruby
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