From: Robert Klemme Date: 2013-07-09T06:37:23+09:00 Subject: Re: A very half-baked idea: setting local variables --047d7bf0c57c608f1204e106d959 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Jul 8, 2013 at 11:06 PM, Hal Fulton wrote: > I'm sure this nonexistent feature is not in great demand, but there have > been different times that I (and other people) have wanted it. > > This is not a "really serious" proposal, but I wanted to see what other > people's comments were about lengthy multiple assignments and > how to deal with them. > > Sometimes I wish I had an alternative way to do assignment. > > Example: > > foo, bar, baz, alpha, beta, gamma, fred, bill, joey = *values > > In a case like that, my eye has to scan a long way to the right > before finding the equal sign and grasping that this is a multiple > assignment. > > I like this better: > > vars = [:foo, :bar, :baz, :alpha, :beta, :gamma, :fred, :bill, :joey] > assign values, vars > > Note that this wouldn't be a method call (like alias_method) but > a keyword (like alias) unless "binding of caller" was permitted... > which is another thing I've wanted but I suppose will never > happen. > > Comments, questions, projectiles? > At the moment there is no other way I am aware of to introduce local variables _at runtime_. While you can do things like eval("foo = 123") the remaining code won't be using the local variable. $ ruby -e 'b=binding;eval("x=10", b);puts x' -e:1:in `
': undefined local variable or method `x' for main:Object (NameError) $ ruby -e 'b=binding;eval("x=10", b);eval("puts x", b)' 10 How about data = {} [:foo, :bar, :baz, :alpha, :beta, :gamma, :fred, :bill, :joey].each_with_index do |s,i| data[s] = values[i] end puts data[:baz] or even data = [:foo, :bar, :baz, :alpha, :beta, :gamma, :fred, :bill, :joey].each_with_index.each_with_object {} do |(s,i), d| d[s] = values[i] end ? Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --047d7bf0c57c608f1204e106d959 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Mon, Jul 8, 2013 at 11:06 PM, Hal Fulton <<= a href=3D"mailto:rubyhacker@gmail.com" target=3D"_blank">rubyhacker@gmail.c= om> wrote:
I'm sure this nonexistent feature is = not in great demand, but there have
been different times that I (and other people) have wanted it.
This is not a "really serious" proposal, but I wante= d to see what other
people's comments were about lengthy multiple assignments and
how to deal with them.

Sometimes I wish I ha= d an alternative way to do assignment.

Example:

=A0 foo, bar, baz, alpha, = beta, gamma, fred, bill, joey =3D *values

In a cas= e like that, my eye has to scan a long way to the right
before finding the equal sign and grasping that this is a multiple
assignment.

I like this better:
<= br>
=A0 vars =3D [:foo, :bar, :baz, :alpha, :beta, :gamma, :fred,= :bill, :joey]
=A0 assign values, vars

Note that this wouldn= 't be a method call (like alias_method) but=A0
a keyword (lik= e alias) unless "binding of caller" was permitted...
which is another thing I've wanted but I suppose will never
<= div>happen.

Comments, questions, projectiles?

At the moment there is no other = way I am aware of to introduce local variables _at runtime_. =A0While you c= an do things like eval("foo =3D 123") the remaining code won'= t be using the local variable.

$ ruby -e 'b=3Dbinding;eval("x=3D10", b);put= s x'
-e:1:in `<main>': undefi= ned local variable or method `x' for main:Object (NameError)
$ ruby -e 'b=3Dbinding;eval("x=3D10", b);eval("= ;puts x", b)'
10


How about

data =3D {}
[:foo, :bar= , :baz, :alpha, :beta, :gamma, :fred, :bill, :joey].each_with_index do |s,i= |
=A0 data[s] =3D values[i]
end

puts data[:baz]

or even

<= div>data =3D [:foo, :bar, :baz, :alpha, :beta, :gamma, :fred, :bill, :joey]= .each_with_index.each_with_object {} do |(s,i), d|
=A0 d[s] =3D values[i]
end

?
<= div>
Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
--047d7bf0c57c608f1204e106d959--