# Adding an argument to a proc

**URL:** <https://rubytalk.org/t/adding-an-argument-to-a-proc/792>\
**Category:** ruby-talk\
**Created:** [22 July 2002 08:36 UTC](https://rubytalk.org/t/adding-an-argument-to-a-proc/792 "2002-07-22T08:36:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Sawyer](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@Tom\_Sawyer](https://rubytalk.org/u/Tom_Sawyer)\
**Post date:** [22 July 2002 08:36 UTC](https://rubytalk.org/t/adding-an-argument-to-a-proc/792/1 "2002-07-22T08:36:00Z")

</div>

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/  
> ^ ^

---

<div class="post-metadata">

**Author:** ![Kent\_Dahl2](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@Kent\_Dahl2](https://rubytalk.org/u/Kent_Dahl2)\
**Post date:** [22 July 2002 10:24 UTC](https://rubytalk.org/t/adding-an-argument-to-a-proc/792/2 "2002-07-22T10:24:33Z")

</div>

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/](http://www.stud.ntnu.no/~kentda/) ]**_/~  
> ))\_student_/(( \__d L b_\_/ NTNU - graduate engineering - 4. year )  
> ( \__\_õ|õ_/**/ ) _)Industrial economics and technological management(  
> \__**/_ö_\_\_\_\_/ (\_**_engineering.discipline_=_Computer::Technology_**)

---

<div class="post-metadata">

**Author:** ![Tom\_Sawyer](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@Tom\_Sawyer](https://rubytalk.org/u/Tom_Sawyer)\
**Post date:** [22 July 2002 10:35 UTC](https://rubytalk.org/t/adding-an-argument-to-a-proc/792/3 "2002-07-22T10:35:39Z")

</div>

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 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/](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/  
> ^ ^

---

<div class="post-metadata">

**Author:** ![Kent\_Dahl2](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@Kent\_Dahl2](https://rubytalk.org/u/Kent_Dahl2)\
**Post date:** [22 July 2002 10:44 UTC](https://rubytalk.org/t/adding-an-argument-to-a-proc/792/4 "2002-07-22T10:44:38Z")

</div>

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/](http://www.stud.ntnu.no/~kentda/) ]**_/~  
> ))\_student_/(( \__d L b_\_/ NTNU - graduate engineering - 4. year )  
> ( \__\_õ|õ_/**/ ) _)Industrial economics and technological management(  
> \__**/_ö_\_\_\_\_/ (\_**_engineering.discipline_=_Computer::Technology_**)
