Class | Proc |
In: |
lib/more/facets/openobject.rb
lib/more/facets/partial.rb |
Parent: | Object |
Convert a Proc object into new partial Proc object.
a = proc { |a,b,c| a+b+c } b = a.partial(X,2,X) b[1,3] #=> 6 a = proc { |a,b,c| a+b+c } b = a.partial(__,2,__) b[1,3] #=> 6
This method is similar to Proc#curry.
CREDT Trans
# File lib/more/facets/partial.rb, line 17 17: def partial(*args) 18: Proc.new do |*spice| 19: result = args.collect do |a| 20: X == a ? spice.pop : a 21: end 22: call(*result) 23: end 24: end
Translates a Proc into an OpenObject. By droping an OpenObject into the Proc, the resulting assignments incured as the procedure is evaluated produce the OpenObject. This technique is simlar to that of MethodProbe.
p = lambda { |x| x.word = "Hello" } o = p.to_openobject o.word #=> "Hello"
NOTE The Proc must have an arity of one —no more and no less.
# File lib/more/facets/openobject.rb, line 260 260: def to_openobject 261: raise ArgumentError, 'bad arity for converting Proc to openobject' if arity != 1 262: o = OpenObject.new 263: self.call( o ) 264: o 265: end