Class: Proc
- Defined in:
- lib/nano/proc/compose.rb,
lib/nano/proc/%2A.rb,
lib/nano/proc/to_method.rb
Overview
– Thanks dave! ++
Constant Summary collapse
Instance Method Summary collapse
- #* ⇒ Object
-
#compose(other) ⇒ Object
Returns a new proc that is the functional compostion of two procs, in order.
-
#to_method(name = nil) ⇒ Object
Creates a local method based on a Proc.
Instance Method Details
#* ⇒ Object
7 |
# File 'lib/nano/proc/%2A.rb', line 7 alias_method( :*, :compose ) |
#compose(other) ⇒ Object
Returns a new proc that is the functional compostion of two procs, in order.
a = lambda { |x| x + 4 }
b = lambda { |y| y / 2 }
(a * b).call(4) #=> 6
(b * a).call(4) #=> 4
15 16 17 18 19 |
# File 'lib/nano/proc/compose.rb', line 15 def compose(other) raise ArgumentError, "arity count mismatch" unless arity == other.arity #proc{ |*a| self.call(other.call(*a)) } proc{ |*a| self.call(*other.call(*a)) } end |
#to_method(name = nil) ⇒ Object
Creates a local method based on a Proc.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/nano/proc/to_method.rb', line 15 def to_method(name=nil) name ||= "!to_method_temp#{self.id}" recv = eval("self", self) klass = recv.class MethodMutexes[klass => name].synchronize do begin klass.send(:define_method, name, &self) return recv.method(name) ensure klass.send(:remove_method, name) if not name end end end |