Methods
partial
to_openobject
Public Instance methods
Convert a Proc object into new partial Proc object.
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
TODO: Parhaps ArgumentError would suffice, and we don‘t need MissingArgument?
[ + ]
# File lib/more/facets/partial.rb, line 27 def partial(*args) Proc.new do |*spice| result = args.collect do |a| MissingArgument == a ? spice.pop : a end call(*result) end 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 def to_openobject raise ArgumentError, 'bad arity for converting Proc to openobject' if arity != 1 o = OpenObject.new self.call( o ) o end