is there a way to add an argument to a proc after its creation?
ex-
p = Proc.new { x + 5 }
px = Proc.new { |x| p.call(5) }
this won’t work
···
–
~transami
(") dobee dobee do…
\v/
^ ^
is there a way to add an argument to a proc after its creation?
ex-
p = Proc.new { x + 5 }
px = Proc.new { |x| p.call(5) }
this won’t work
–
~transami
(") dobee dobee do…
\v/
^ ^
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)
Kent,
yuk, that is nasty! neat hack though. i’ll see if i can just make do
with having to have the argument - its only a minor inconvience.
have any ideas about my super in proc post? that one is kind of vital. i
don’t know what i’ll do if i can’t figure that one out.
thanks!
~tom
On Mon, 2002-07-22 at 04:24, Kent Dahl wrote:
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 scopeex-
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)
–
~transami
(") dobee dobee do…
\v/
^ ^
Kent Dahl wrote:
p2 = Proc.new {|y_uNiQuE_VarName|
Ops. You don’t have to go nuts on the y like this. I was trying
something and forgot to change the y name back.
–
([ 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)