From: Kent Dahl Date: 2002-07-22T19:24:33+09:00 Subject: Re: adding an argument to a proc Tom Sawyer wrote: > > is there a way to add an argument to a proc after its creation? Don't think so. Sounds like it would wreak a havoc of ambiguity on variable scope > ex- > > p = Proc.new { x + 5 } > px = Proc.new { |x| p.call(5) } > > this won't work You could perhaps be nasty and run eval on the binding of the block and setting the scope local variable explicitly, before calling the proc. x = nil # Has to exist in scope of block definition p1 = Proc.new { x + 5 } # Define a wrapper Proc that sets the x to a number p2 = Proc.new {|y_uNiQuE_VarName| eval "old_x_uNiQuE_VarName = x", p1 eval "x=#{y_uNiQuE_VarName}", p1 # naive version result = p1.call eval "x=old_x_uNiQuE_VarName", p1 result } # Less naive version that should work with any object p3 = Proc.new {|y_uNiQuE_VarName| eval "old_x_uNiQuE_VarName = x", p1 eval "x=ObjectSpace._id2ref(#{y_uNiQuE_VarName.id})", p1 # less naive result = p1.call eval "x=old_x_uNiQuE_VarName", p1 result } puts p2.call(5) #=> 10 puts p3.call(15) #)=> 20 puts "Dang!" if x != nil -- (\[ Kent Dahl ]/)_ _~_ __[ http://www.stud.ntnu.no/~kentda/ ]___/~ ))\_student_/(( \__d L b__/ NTNU - graduate engineering - 4. year ) ( \__\_�|�_/__/ ) _)Industrial economics and technological management( \____/_�_\____/ (____engineering.discipline_=_Computer::Technology___)