Metapogramming

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

Hi Berg,

maybe this Stackoverflow post might help you:

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;

(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
PGP: 0x6B3A101A

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;

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/

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;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;