# Metapogramming

**URL:** <https://rubytalk.org/t/metapogramming/73680>\
**Category:** ruby-talk\
**Created:** [24 June 2016 23:40 UTC](https://rubytalk.org/t/metapogramming/73680 "2016-06-24T23:40:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_Berger](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@A\_Berger](https://rubytalk.org/u/A_Berger)\
**Post date:** [24 June 2016 23:40 UTC](https://rubytalk.org/t/metapogramming/73680/1 "2016-06-24T23:40:34Z")

</div>

How can I declare many vars at once?  
a=b=c=[1,2] # is no good idea

vars=['a','b','c']  
vars.each { |v| eval v=[v] }  
How can I set the binding, so that these vars are set???

Thank you \<\< "for ?!"  
Berg

---

<div class="post-metadata">

**Author:** ![Paul\_Gotze](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/paul_gotze/32/2048_2.png) [@Paul\_Gotze](https://rubytalk.org/u/Paul_Gotze)\
**Post date:** [25 June 2016 00:34 UTC](https://rubytalk.org/t/metapogramming/73680/2 "2016-06-25T00:34:42Z")

</div>

Hi Berg,

maybe this Stackoverflow post might help you:

> <https://stackoverflow.com/questions/18552891/how-to-dynamically-create-a-local-variable/18552991#18552991>

Dependent on your use case, you should maybe also consider to use a  
separate data structure (like a Hash) for storing the actual values instead  
of creating local variables.

Best,  
Paul

> **···**
>
> 2016-06-25 1:40 GMT+02:00 A Berger \<aberger7890@gmail.com\>:
> 
> > How can I declare many vars at once?  
> > a=b=c=[1,2] # is no good idea
> > 
> > vars=['a','b','c']  
> > vars.each { |v| eval v=[v] }  
> > How can I set the binding, so that these vars are set???
> > 
> > Thank you \<\< "for ?!"  
> > Berg
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [25 June 2016 09:22 UTC](https://rubytalk.org/t/metapogramming/73680/3 "2016-06-25T09:22:29Z")

</div>

(why?)

The simplest option would be to use a hash (or even an array)  
and avoid using lots of local variables, unless you have a  
very special use case where that's not possible.

Regards,  
Marcus

> **···**
>
> Am 25.06.2016 um 01:40 schrieb A Berger:
> 
> > How can I declare many vars at once?  
> > a=b=c=[1,2] # is no good idea
> 
> --  
> GitHub: [stomar (Marcus Stollsteimer) · GitHub](https://github.com/stomar/)  
> PGP: 0x6B3A101A

---

<div class="post-metadata">

**Author:** ![Xeno\_Campanoli1](https://avatars.discourse-cdn.com/v4/letter/x/85f322/32.png) [@Xeno\_Campanoli1](https://rubytalk.org/u/Xeno_Campanoli1)\
**Post date:** [26 June 2016 17:42 UTC](https://rubytalk.org/t/metapogramming/73680/4 "2016-06-26T17:42:20Z")

</div>

That is sometimes helpful, especially in Unit Testing, but I tend to  
recommend against it. If you have too much stuff like that, you may  
be best thinking harder on your design

> **···**
>
> On Fri, Jun 24, 2016 at 4:40 PM, A Berger \<aberger7890@gmail.com\> wrote:
> 
> > How can I declare many vars at once?  
> > a=b=c=[1,2] # is no good idea
> > 
> > vars=['a','b','c']  
> > vars.each { |v| eval v=[v] }  
> > How can I set the binding, so that these vars are set???
> > 
> > Thank you \<\< "for ?!"  
> > Berg
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<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:** [25 June 2016 10:10 UTC](https://rubytalk.org/t/metapogramming/73680/5 "2016-06-25T10:10:20Z")

</div>

It's interesting that this comes up again and again. People do not  
seem to understand that local variables that they somehow get inserted  
in the local binding (potentially with generated names) can only be  
accessed in a generic way (i.e. generating variable names again or  
iterating through all variables present). If you do that, using a Hash  
or Array is \_way\_ easier and readable and less error prone.

Put it differently, there is no point in doing

some\_magic\_function\_to\_define\_local\_variables(get\_variable\_names\_from\_somewhere())  
...  
puts a # we know it's there

Kind regards

robert

> **···**
>
> On Sat, Jun 25, 2016 at 11:22 AM, \<sto.mar@web.de\> wrote:
> 
> > Am 25.06.2016 um 01:40 schrieb A Berger:
> > 
> > > How can I declare many vars at once?  
> > > a=b=c=[1,2] # is no good idea
> > 
> > (why?)
> > 
> > The simplest option would be to use a hash (or even an array)  
> > and avoid using lots of local variables, unless you have a  
> > very special use case where that's not possible.
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![abinoam](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/abinoam/32/1887_2.png) [@abinoam](https://rubytalk.org/u/abinoam)\
**Post date:** [26 June 2016 21:28 UTC](https://rubytalk.org/t/metapogramming/73680/6 "2016-06-26T21:28:28Z")

</div>

Hi Berg (and all),

Array and Hash are your friends  
Struct and OpenStruct are your friends, with superpowers!

Best regards,  
Abinoam Jr.

> **···**
>
> On Sun, Jun 26, 2016 at 2:42 PM, Xeno Campanoli \<xeno.campanoli@gmail.com\> wrote:
> 
> > That is sometimes helpful, especially in Unit Testing, but I tend to  
> > recommend against it. If you have too much stuff like that, you may  
> > be best thinking harder on your design
> > 
> > On Fri, Jun 24, 2016 at 4:40 PM, A Berger \<aberger7890@gmail.com\> wrote:
> > 
> > > How can I declare many vars at once?  
> > > a=b=c=[1,2] # is no good idea
> > > 
> > > vars=['a','b','c']  
> > > vars.each { |v| eval v=[v] }  
> > > How can I set the binding, so that these vars are set???
> > > 
> > > Thank you \<\< "for ?!"  
> > > Berg
> > > 
> > > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
