Sha256: 02e88c31036a5e3e9d7b22806b9a9b9ab2ed4c0e61609bfce152ecf1e00fa4bb
Contents?: true
Size: 578 Bytes
Versions: 9
Compression:
Stored size: 578 Bytes
Contents
# frozen_string_literal: true module Memoize class ArgsError < StandardError; end module ClassMethods def memoize(name) ivar = :"@#{name.to_s.sub('?', '_predicate')}" prepend Module.new { define_method(name) do |*args| raise ArgsError, 'cannot pass arguments to memoized method %p' % name unless args.empty? return instance_variable_get(ivar) if instance_variable_defined?(ivar) instance_variable_set(ivar, super()) end } end end def self.included(base) base.extend(ClassMethods) end end
Version data entries
9 entries across 9 versions & 2 rubygems