Class: Method::Composition

Inherits:
Object show all
Defined in:
lib/core/facets/method/composition.rb

Overview

Method Composition class acts a proxy for composed methods.

Author:

  • Mike Burns

Instance Method Summary collapse

Constructor Details

#initialize(*f) ⇒ Composition

Returns a new instance of Composition.



14
15
16
# File 'lib/core/facets/method/composition.rb', line 14

def initialize(*f)
  @f = f
end

Instance Method Details

#*(h) ⇒ Object



25
26
27
28
# File 'lib/core/facets/method/composition.rb', line 25

def *(h)
  #Composition.new(self, h)
  @f << h
end

#[](*x) ⇒ Object



71
72
73
# File 'lib/core/facets/method/composition.rb', line 71

def [](*x)
  call(*x)
end

#^(n) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
# File 'lib/core/facets/method/composition.rb', line 31

def ^(n)
  raise ArgumentError if n < 0

  return self if n < 2

  #Composition.new(self, self ^ (n-1))
  (n - 1).times{ @f = @f.concat(@f) }
end

#arityObject



51
52
53
# File 'lib/core/facets/method/composition.rb', line 51

def arity
  @f.last.arity
end

#call(*x) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/core/facets/method/composition.rb', line 62

def call(*x)
  r = x
  @f.reverse_each do |f|
    r = f.call(*r)
  end
  r
end

#inspectObject



19
20
21
22
# File 'lib/core/facets/method/composition.rb', line 19

def inspect
  x = @f.map{ |f| f.inspect }.join(' * ')
  "#<Method::Composition: #{x}>"
end

#ownerObject



41
42
43
# File 'lib/core/facets/method/composition.rb', line 41

def owner
  @f.last.owner
end

#receiverObject



46
47
48
# File 'lib/core/facets/method/composition.rb', line 46

def receiver
  @f.first.receiver
end

#to_procObject



56
57
58
59
# File 'lib/core/facets/method/composition.rb', line 56

def to_proc
  #Proc.new {|x| @f.call(*@g.call(*x)) }
  Proc.new { |*x| call(*x) }
end