Module: Metaprogrammer::Usages
- Included in:
- Metaprogrammer
- Defined in:
- lib/metaprogrammer/usages.rb
Instance Method Summary collapse
- #memoize(klass: self, **opts, &blk) ⇒ Object
- #rescue_all_methods(*exceptions, klass: self, **opts, &blk) ⇒ Object
Instance Method Details
#memoize(klass: self, **opts, &blk) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/metaprogrammer/usages.rb', line 14 def memoize(klass: self, **opts, &blk) patch_all_methods(klass: klass, **opts) do |orig_method, *args, &caller_blk| memos = @_memoized_methods ||= {} method_memos = @_memoized_methods[orig_method] ||= {} method_memos[args] ||= orig_method.call *args, &caller_blk end end |
#rescue_all_methods(*exceptions, klass: self, **opts, &blk) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/metaprogrammer/usages.rb', line 4 def rescue_all_methods(*exceptions, klass: self, **opts, &blk) patch_all_methods(klass: klass, **opts) do |orig_method, *args, &caller_blk| begin orig_method.call *args, &caller_blk rescue *exceptions => e blk.call(e) end end end |