Skip to content

Instantly share code, notes, and snippets.

@3014zhangshuo
Created March 9, 2019 13:00
Show Gist options
  • Save 3014zhangshuo/7bb4da43a9fd3ab92292966a8cee3e76 to your computer and use it in GitHub Desktop.
Save 3014zhangshuo/7bb4da43a9fd3ab92292966a8cee3e76 to your computer and use it in GitHub Desktop.
ruby memoized
module RubyMemoized
class Memoizer
def initialize(content, method)
@content = content
@method = method
end
def call(*args, &block)
return cache[[args, block]] if cache.has_key?(args, block])
cache[[args, block]] = content.send(method, *args, &block)
end
def cache
@cache ||= {}
end
end
def self.included(klass)
klass.extend(ClassMethods)
end
module ClassMethods
def memoized
@memoized = true
end
def unmemoized
@memoized = false
end
def method_memoized(method)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment