Class: Functor

Inherits:
Object show all
Defined in:
lib/functor.rb

Defined Under Namespace

Modules: Method

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Functor

Returns a new instance of Functor.



30
31
32
# File 'lib/functor.rb', line 30

def initialize( &block ) 
  @rules = [] ; instance_eval( &block ) if block_given?
end

Instance Method Details

#apply(object, *args, &block) ⇒ Object



39
40
41
# File 'lib/functor.rb', line 39

def apply( object, *args, &block )
  object.instance_exec( *args, &match( args, &block ) )
end

#call(*args, &block) ⇒ Object



43
44
45
# File 'lib/functor.rb', line 43

def call( *args, &block )
  match( args, &block ).call( *args )
end

#given(*pattern, &action) ⇒ Object



34
35
36
37
# File 'lib/functor.rb', line 34

def given( *pattern, &action )
  @rules.delete_if { |p,a| p == pattern }
  @rules << [ pattern, action ]
end

#to_procObject



47
# File 'lib/functor.rb', line 47

def to_proc ; lambda { |*args| self.call( *args ) } ; end