Class: Proc

Inherits:
Object show all
Defined in:
lib/epitools/core_ext/misc.rb

Instance Method Summary collapse

Instance Method Details

#chain(other = nil, &block) ⇒ Object Also known as: |

Chains two procs together, returning a new proc. The output from each proc is passed into the input of the next one.

Example:

chain = proc { 1 } | proc { |input| input + 1 }
chain.call #=> 2


90
91
92
93
# File 'lib/epitools/core_ext/misc.rb', line 90

def chain(other=nil, &block)
  other ||= block
  proc { |*args| other.call( self.call(*args) ) }
end

#join(other = nil, &block) ⇒ Object Also known as: &

Join two procs together, returning a new proc. Each proc is executed one after the other, with the same input arguments. The return value is an array of the return values from all the procs.

You can use either the .join method, or the overloaded & operator.

Examples:

joined = proc1 & proc2
joined = proc1.join proc2
newproc = proc { 1 } & proc { 2 }
newproc.call #=> [1, 2]


76
77
78
79
# File 'lib/epitools/core_ext/misc.rb', line 76

def join(other=nil, &block)
  other ||= block
  proc { |*args| [self.call(*args), other.call(*args)] }
end