From: Matthew Kerwin Date: 2015-11-16T07:04:52+10:00 Subject: [ruby-core:71499] Re: [Ruby trunk - Feature #11690] [Open] Update Hash during multiple assignment --001a1135ae12d67cea05249aa468 Content-Type: text/plain; charset=UTF-8 On 16/11/2015 2:32 AM, <6ftdan@gmail.com> wrote: > > This would be most useful in scenarios where a method or proc return multiple values. When the method returns the values we don't normally know the key outside where the hash assignment is. > > ~~~ruby > example = proc { [{:hi => :hello}, 5] } > > hash = {} > > # Currently in Ruby with an Unknown key multiple assignment isn't an option > hash[????], current = example.call > > # We currently have to two step it > result, current = example.call > hash.update(result) > ~~~ > > But with `Hash#update=` we don't have to know the key. > I get the use-case, but I think the understandability of the code starts to suffer. What about something completely new but splat-like? hash[*], current = example.call This is even better when the hash comes last, it looks more like an options parameter: opts = get_default_hash a, b, opts[*] = example2.call I would assume this also works in single assignment: opts = get_default_hash opts[*] = get_new_opts --001a1135ae12d67cea05249aa468 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable


On 16/11/2015 2:32 AM, <6ftdan@gmail= .com> wrote:
>
> This would be most useful in scenarios where a method or proc return m= ultiple values.=C2=A0 When the method returns the values we don't norma= lly know the key outside where the hash assignment is.
>
> ~~~ruby
> example =3D proc { [{:hi =3D> :hello}, 5] }
>
> hash =3D {}
>
> # Currently in Ruby with an Unknown key multiple assignment isn't = an option
> hash[????], current =3D example.call
>
> # We currently have to two step it
> result, current =3D example.call
> hash.update(result)
> ~~~
>
> But with `Hash#update=3D` we don't have to know the key.
>

I get the use-case, but I think the understandability of the= code starts to suffer.

What about something completely new but splat-like?

=C2=A0=C2=A0=C2=A0 hash[*], current =3D example.call

This is even better when the hash comes last, it looks more = like an options parameter:

=C2=A0=C2=A0=C2=A0 opts =3D get_default_hash
=C2=A0=C2=A0=C2=A0 a, b, opts[*] =3D example2.call

I would assume this also works in single assignment:

=C2=A0=C2=A0=C2=A0 opts =3D get_default_hash
=C2=A0=C2=A0=C2=A0 opts[*] =3D get_new_opts

--001a1135ae12d67cea05249aa468--