From: Joseph Jones Date: 2015-12-17T21:06:25-07:00 Subject: [ruby-core:72269] [Ruby trunk - Feature #11747] \"bury\" feature, similar to 'dig' but opposite --56738641_4516dde9_16c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Joseph Jones liked your message with Boxer. On December 2, 2015 at 16:36:= 12 MST, dameyawn=40gmail.com wrote:Issue =2311747 has been updated by dam= ien sutevski.Tsuyoshi Sawada wrote:> > inferred from the what the user is= passing (such as a symbol or string for a hash or an integer for an arra= y)> > I don't think this is a good idea. I think it should rather depend = on the class of the receiver.> > =7B=7D.bury(:users, 0, :name, 'Matz') =23= =3D> =7B:users =3D> =7B0 =3D> =7B:name =3D> =22Matz=22=7D=7D=7D> =5B=5D.= bury(:users, 0, :name, 'Matz') =23 =3D> error> =7B=7D.bury(0, 1, 2, :foo)= =23 =3D> =7B0 =3D> =7B1 =3D> =7B2 =3D> :foo=7D=7D=7D> =5B=5D.bury(0, 1, = 2, :foo) =23 =3D> =5B=5Bnil, =5Bnil, nil, :foo=5D=5D=5D> > and similar fo= r struct.I agree. I should clarify that I was assuming the class of the r= eceiver (=60data=60) was known in my example. The inference I was talking= about was that a buried =600=60 would imply an array position by default= instead of a hash key.----------------------------------------=46eature = =2311747: =22bury=22 feature, similar to 'dig' but opposite https://bugs.= ruby-lang.org/issues/11747=23change-55211* Author: damien sutevski* Statu= s: =46eedback* Priority: Normal* Assignee: Yukihiro Matsumoto------------= ----------------------------In Matz's recent Rubyconf talk, he used this = example for the new 'dig' feature coming in Ruby 2.3:=7E=7E=7Eruby=23 we = want thisdata=5B:users=5D=5B0=5D=5B:name=5D=23 we can do this w/o nil err= orsdata.dig(:users, 0, :name)=7E=7E=7EWhat I'm proposing is a 'bury' feat= ure that is the opposite of 'dig' in a sense. It inserts a value at an ar= bitrary depth, for example:=7E=7E=7Erubydata.bury(:users, 0, :name, 'Matz= ')=7E=7E=7EThis will create a nested hash or an array automatically at ea= ch step if it doesn't already exist, and that can be inferred from the wh= at the user is passing (such as a symbol or string for a hash or an integ= er for an array). It's similar to autovivification but more powerful=21Th= is behavior is very common, at least in my experience, so a dry method bu= ilt into Ruby would be awesome=21 -- https://bugs.ruby-lang.org/ --56738641_4516dde9_16c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Joseph Jones liked your message with Boxer.


= On December 2, 2015 at 16:36:12 MST, dameyawn=40gmail.com wrote:
Issue =2311747 has been update= d by damien sutevski.


Tsuyoshi Sawada wrote:
> > i= nferred from the what the user is passing (such as a symbol or string for= a hash or an integer for an array)
>
> I don't think this is = a good idea. I think it should rather depend on the class of the receiver= .
>
> =7B=7D.bury(:users, 0, :name, 'Matz') =23 =3D> =7B:u= sers =3D> =7B0 =3D> =7B:name =3D> =22Matz=22=7D=7D=7D
> =5B=5D.b= ury(:users, 0, :name, 'Matz') =23 =3D> error
> =7B=7D.bury(0, 1,= 2, :foo) =23 =3D> =7B0 =3D> =7B1 =3D> =7B2 =3D> :foo=7D=7D=7D
> = =5B=5D.bury(0, 1, 2, :foo) =23 =3D> =5B=5Bnil, =5Bnil, nil, :foo=5D=5D=5D=
>
> and similar for struct.

I agree. I should clar= ify that I was assuming the class of the receiver (=60data=60) was known = in my example. The inference I was talking about was that a buried =600=60= would imply an array position by default instead of a hash key.



----------------------------------------
=46ea= ture =2311747: =22bury=22 feature, similar to 'dig' but opposite
ht= tps://bugs.ruby-lang.org/issues/11747=23change-55211

* Author:= damien sutevski
* Status: =46eedback
* Priority: Normal
*= Assignee: Yukihiro Matsumoto
--------------------------------------= --
In Matz's recent Rubyconf talk, he used this example for the new = 'dig' feature coming in Ruby 2.3:

=7E=7E=7Eruby
=23 we wa= nt this
data=5B:users=5D=5B0=5D=5B:name=5D

=23 we can do = this w/o nil errors
data.dig(:users, 0, :name)
=7E=7E=7E
<= br />What I'm proposing is a 'bury' feature that is the opposite of 'dig'= in a sense. It inserts a value at an arbitrary depth, for example:
=
=7E=7E=7Eruby
data.bury(:users, 0, :name, 'Matz')
=7E=7E=7E=

This will create a nested hash or an array automatically at e= ach step if it doesn't already exist, and that can be inferred from the w= hat the user is passing (such as a symbol or string for a hash or an inte= ger for an array). It's similar to autovivification but more powerful=21<= br />
This behavior is very common, at least in my experience, so a = dry method built into Ruby would be awesome=21



--=
https://bugs.ruby-lang.org/
<= /html> --56738641_4516dde9_16c--