Class: Module

### Home Classes Methods

### In Files

- lib/spruz/memoize.rb
- lib/spruz/xt/named.rb

### Parent

Object

### Methods

- ::__memoize_cache__
- ::__memoize_cache_delete__
- #__memoize_cache__
- #memoize_cache_clear
- #memoize_function
- #memoize_method
- #named

### Included Modules

- Module

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Spruz
- Spruz::Attempt
- Spruz::Bijection
- Spruz::Blank
- Spruz::Blank::Array
- Spruz::Blank::FalseClass
- Spruz::Blank::Hash
- Spruz::Blank::NilClass
- Spruz::Blank::Numeric
- Spruz::Blank::Object
- Spruz::Blank::String
- Spruz::Blank::TrueClass
- Spruz::CountBy
- Spruz::DeepDup
- Spruz::FileBinary
- Spruz::FileBinary::ClassMethods
- Spruz::FileBinary::Constants
- Spruz::Full
- Spruz::GO
- Spruz::Generator
- Spruz::HashSymbolizeKeysRecursive
- Spruz::HashUnion
- Spruz::Limited
- Spruz::LinesFile
- Spruz::LinesFile::LineExtension
- Spruz::Memoize
- Spruz::Minimize
- Spruz::ModuleGroup
- Spruz::Null
- Spruz::Once
- Spruz::P
- Spruz::PartialApplication
- Spruz::RangePlus
- Spruz::Round
- Spruz::SecureWrite
- Spruz::Shuffle
- Spruz::StringCamelize
- Spruz::StringUnderscore
- Spruz::StringVersion
- Spruz::StringVersion::Version
- Spruz::Subhash
- Spruz::TimeDummy
- Spruz::TimeDummy::ClassMethods
- Spruz::ToProc
- Spruz::UniqBy
- Spruz::Write
- Array
- Enumerable
- File
- Float
- Hash
- IO
- IRB
- Integer
- Method
- Module
- Object
- Proc
- Range
- String
- Symbol
- Time

No matching classes.

# Module

### Public Class Methods

__memoize_cache__() click to toggle source

Returns the current memoize cache for all the stored objects and method call results.

    # File lib/spruz/memoize.rb, line 7
    def __memoize_cache__
      @__memoize_cache__ ||= {}
    end

__memoize_cache_delete__() click to toggle source

Finalizer to delete the stored result for a garbage collected object.

    # File lib/spruz/memoize.rb, line 12
    def __memoize_cache_delete__
      lambda do |id|
        $DEBUG and warn "Deleted method results for object id='#{id}'"
        __memoize_cache__.delete(id)
      end
    end

### Public Instance Methods

__memoize_cache__() click to toggle source

Returns the current memoize cache for this Module.

    # File lib/spruz/memoize.rb, line 44
    def __memoize_cache__
      @__memoize_cache__
    end

memoize_cache_clear() click to toggle source

Clear cached values for all methods and functions.

    # File lib/spruz/memoize.rb, line 69
    def memoize_cache_clear
      mc = @__memoize_cache__ and mc.clear
      mc = ::Module.__memoize_cache__ and mc.clear
      self
    end

memoize_function(*function_ids) click to toggle source

Automatically memoize calls of the functions `function_ids`. The memoized result does ONLY depend on the arguments given to the function.

    # File lib/spruz/memoize.rb, line 51
    def memoize_function(*function_ids)
      mc = @__memoize_cache__ ||= {}
      function_ids.each do |method_id|
        method_id = method_id.to_s.to_sym
        orig_method = instance_method(method_id)
        __send__(:define_method, method_id) do |*args|
          if mc.key?(method_id) and result = mc[method_id][args]
            result
          else
            (mc[method_id] ||= {})[args] = result = orig_method.bind(self).call(*args)
            $DEBUG and warn "#{self.class} cached function #{method_id}(#{args.inspect unless args.empty?}) = #{result.inspect}"
          end
          result
        end
      end
    end

memoize_method(*method_ids) click to toggle source

Automatically memoize calls of the the methods `method_ids`. The memoized results do NOT ONLY depend on the arguments, but ALSO on the object the method is called on.

    # File lib/spruz/memoize.rb, line 23
    def memoize_method(*method_ids)
      method_ids.each do |method_id|
        method_id = method_id.to_s.to_sym
        orig_method = instance_method(method_id)
        __send__(:define_method, method_id) do |*args|
          unless mc = ::Module.__memoize_cache__[__id__]
            mc = ::Module.__memoize_cache__[__id__] ||= {}
            ObjectSpace.define_finalizer(self, ::Module.__memoize_cache_delete__)
          end
          if mc.key?(method_id) and result = mc[method_id][args]
            result
          else
            (mc[method_id] ||= {})[args] = result = orig_method.bind(self).call(*args)
            $DEBUG and warn "#{self.class} cached method #{method_id}(#{args.inspect unless args.empty?}) = #{result.inspect} [#{__id__}]"
          end
          result
        end
      end
    end

named(name, method, *args, &named_block) click to toggle source

    # File lib/spruz/xt/named.rb, line 22
    def named(name, method, *args, &named_block)
      include Module.new {
        define_method(name) do |*rest, &block|
          block = named_block if named_block
          __send__(method, *(args + rest), &block)
        end
      }
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.