Created
March 9, 2019 13:00
-
-
Save 3014zhangshuo/7bb4da43a9fd3ab92292966a8cee3e76 to your computer and use it in GitHub Desktop.
ruby memoized
This file contains hidden or 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 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