Created
February 5, 2020 14:41
-
-
Save bossiernesto/9d20935c56da4095eb5963428e14e1b8 to your computer and use it in GitHub Desktop.
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 Memoization | |
| def memoize(name) | |
| @@lookup ||= Hash.new { |h, k| h[k] = {} } | |
| fx = instance_method(name) | |
| define_method(name) do |args| | |
| return @@lookup[name][args] if @@lookup[name].include?(args) | |
| @@lookup[name][args] = fx.bind(self).call(args) | |
| end | |
| end | |
| end | |
| class Bleh | |
| extend Memoization | |
| memoize def saraza(a) | |
| 2*a | |
| end | |
| end | |
| a = Bleh.new | |
| a.saraza(24) | |
| a.class.class_variable_get(:@@lookup) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment