Class: Functor
Defined Under Namespace
Modules: Method
Instance Method Summary collapse
- #apply(object, *args, &block) ⇒ Object
- #call(*args, &block) ⇒ Object
- #given(*pattern, &action) ⇒ Object
-
#initialize(&block) ⇒ Functor
constructor
A new instance of Functor.
- #to_proc ⇒ Object
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 46 |
# File 'lib/functor.rb', line 43 def call( *args, &block ) args.push( block ) if block_given? 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_proc ⇒ Object
48 |
# File 'lib/functor.rb', line 48 def to_proc ; lambda { |*args| self.call( *args ) } ; end |