# Create variables with caller scope

**URL:** <https://rubytalk.org/t/create-variables-with-caller-scope/43873>\
**Category:** ruby-talk\
**Created:** [18 January 2008 11:01 UTC](https://rubytalk.org/t/create-variables-with-caller-scope/43873 "2008-01-18T11:01:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jari\_Williamsson](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@Jari\_Williamsson](https://rubytalk.org/u/Jari_Williamsson)\
**Post date:** [18 January 2008 11:01 UTC](https://rubytalk.org/t/create-variables-with-caller-scope/43873/1 "2008-01-18T11:01:40Z")

</div>

Is it possible for a method to create new variables that becomes in the scope of the caller (instead of the scope of the method)? I guess I mean to get a method to behave much like macros or templates in C/C++.

Something like this:

> **···**
>
> ---  
> def create\_some\_variables(varname1, varname2)  
> &nbsp;&nbsp;&nbsp;&nbsp;# Create the variables and initialize with values  
> end
> 
> create\_some\_variables("a", "b")  
> # Display the values of those variables  
> puts a  
> puts b  
> ---
> 
> mvh/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Jari W.

---

<div class="post-metadata">

**Author:** ![Jean-Francois\_Tran](https://avatars.discourse-cdn.com/v4/letter/j/c89c15/32.png) [@Jean-Francois\_Tran](https://rubytalk.org/u/Jean-Francois_Tran)\
**Post date:** [18 January 2008 12:15 UTC](https://rubytalk.org/t/create-variables-with-caller-scope/43873/2 "2008-01-18T12:15:17Z")

</div>

Jari Williamsson :

> Is it possible for a method to create new variables that  
> becomes in the scope of the caller (instead of the scope  
> of the method)? I guess I mean to get a method to behave  
> much like macros or templates in C/C++.

Why not using a block ?

> Something like this:  
> ---  
> def create\_some\_variables(varname1, varname2)  
> &nbsp;&nbsp;&nbsp;&nbsp;# Create the variables and initialize with values  
> end

def create\_some\_variables(\*args)  
&nbsp;&nbsp;# Create object\_1 and object\_2 and initialize with values  
&nbsp;&nbsp;# ...  
&nbsp;&nbsp;# pass them to the block  
&nbsp;&nbsp;yield object\_1, object\_2 if block\_given?  
end

> create\_some\_variables("a", "b")  
> # Display the values of those variables  
> puts a  
> puts b

create\_some\_variables(...) do |a,b|  
&nbsp;&nbsp;# Display the values of those variables  
&nbsp;&nbsp;puts a  
&nbsp;&nbsp;puts b

&nbsp;&nbsp;# do something with a and b  
&nbsp;&nbsp;# ...  
end

&nbsp;&nbsp;&nbsp;&nbsp;-- Jean-François.

---

<div class="post-metadata">

**Author:** ![Jari\_Williamsson](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@Jari\_Williamsson](https://rubytalk.org/u/Jari_Williamsson)\
**Post date:** [18 January 2008 12:27 UTC](https://rubytalk.org/t/create-variables-with-caller-scope/43873/3 "2008-01-18T12:27:33Z")

</div>

Jean-François Trân wrote:

> Jari Williamsson :
> 
> > Is it possible for a method to create new variables that  
> > becomes in the scope of the caller (instead of the scope  
> > of the method)? I guess I mean to get a method to behave  
> > much like macros or templates in C/C++.
> 
> Why not using a block ?

Because the created variables will "only" get block scope, if I understand your sample code correctly. And calling the create\_some\_variables() multiple times would create multiple stacked blocks.

But giving it a bit more thought, I think I should redesign the code a bit and let the method instead dynamically create a class where all the created variables are stored and then returned an instance of that class.

Best regards,

Jari Williamsson

> **···**
>
> > > Something like this:  
> > > ---  
> > > def create\_some\_variables(varname1, varname2)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;# Create the variables and initialize with values  
> > > end
> > 
> > def create\_some\_variables(\*args)  
> > &nbsp;&nbsp;# Create object\_1 and object\_2 and initialize with values  
> > &nbsp;&nbsp;# ...  
> > &nbsp;&nbsp;# pass them to the block  
> > &nbsp;&nbsp;yield object\_1, object\_2 if block\_given?  
> > end
> > 
> > > create\_some\_variables("a", "b")  
> > > # Display the values of those variables  
> > > puts a  
> > > puts b
> > 
> > create\_some\_variables(...) do |a,b|  
> > &nbsp;&nbsp;# Display the values of those variables  
> > &nbsp;&nbsp;puts a  
> > &nbsp;&nbsp;puts b
> > 
> > &nbsp;&nbsp;# do something with a and b  
> > &nbsp;&nbsp;# ...  
> > end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;-- Jean-François.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [18 January 2008 12:37 UTC](https://rubytalk.org/t/create-variables-with-caller-scope/43873/4 "2008-01-18T12:37:29Z")

</div>

OpenStruct and Hash come to mind. One of those is usually far better  
than the hack you attempted initially. Btw, there is another issue  
with this: local variables created dynamically in a scope cannot be  
used directly (i.e. the same way as if they were defined in the scope  
directly):

$ ruby -e 'def f() eval("x=1", binding); p x end; f'  
-e:1:in `f': undefined local variable or method `x' for main:Object (NameError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from -e:1  
13:35:55 /cygdrive/c/SCMws/RKlemme/OPSC\_Gold\_bas\_dev\_R1.2\_perf/bas  
$ ruby -e 'def f() eval("x=1", binding); p(eval("x", binding)) end; f'  
1  
13:36:27 /cygdrive/c/SCMws/RKlemme/OPSC\_Gold\_bas\_dev\_R1.2\_perf/bas

So you need to use some name passing mechanism anyway. And if you do  
that you can use a Hash immediately which is much easier and clearer.

Kind regards

robert

> **···**
>
> 2008/1/18, Jari Williamsson \<jari.williamsson@mailbox.swipnet.se\>:
> 
> > Jean-François Trân wrote:  
> > \> Jari Williamsson :  
> > \>\> Is it possible for a method to create new variables that  
> > \>\> becomes in the scope of the caller (instead of the scope  
> > \>\> of the method)? I guess I mean to get a method to behave  
> > \>\> much like macros or templates in C/C++.  
> > \>  
> > \> Why not using a block ?
> > 
> > Because the created variables will "only" get block scope, if I  
> > understand your sample code correctly. And calling the  
> > create\_some\_variables() multiple times would create multiple stacked blocks.
> > 
> > But giving it a bit more thought, I think I should redesign the code a  
> > bit and let the method instead dynamically create a class where all the  
> > created variables are stored and then returned an instance of that class.
> 
> --  
> use.inject do |as, often| as.you\_can - without end
