Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/omega/memoize.rb,
lib/omega/descendants.rb

Instance Method Summary collapse

Instance Method Details

#class_descendantsObject



6
7
8
# File 'lib/omega/descendants.rb', line 6

def class_descendants
  ObjectSpace.each_object(Class).select {|c| c < self}
end

#cmemoize(n, *m) ⇒ Object



19
20
21
# File 'lib/omega/memoize.rb', line 19

def cmemoize n, *m
  singleton_class.memoize n, *m
end

#descendantsObject



2
3
4
# File 'lib/omega/descendants.rb', line 2

def descendants
  ObjectSpace.each_object(Module).select {|m| m < self}
end

#memoize(n, *arr) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/omega/memoize.rb', line 2

def memoize n, *arr
  ([n] + arr).each do |m|
    meth = instance_method(m)
    hash = Hash.new
    define_method m do |*args|
      if hash.include? args
        hash[args]
      else
        meth = meth.bind(self) if meth.is_a? UnboundMethod
        v = hash[args] = meth.call(*args)
        v.freeze
        v
      end
    end
  end
end