From: Billy E Brand Date: 2015-12-18T07:58:19-05:00 Subject: [ruby-core:72366] Re: [Ruby trunk - Feature #11747] \"bury\" feature, similar to 'dig' but opposite --Apple-Mail-A0F510F5-5696-4BEC-B513-A977A91CA918 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Remove me from mail list please=20 Sent from my iPhone Billy E Brand 404-597-9615 > On Dec 17, 2015, at 23:06, Joseph Jones wrote: >=20 > Joseph Jones liked your message with Boxer. >=20 >=20 >> On December 2, 2015 at 16:36:12 MST, dameyawn@gmail.com wrote: >> Issue #11747 has been updated by damien sutevski. >>=20 >>=20 >> Tsuyoshi Sawada wrote: >> > > inferred from the what the user is passing (such as a symbol or strin= g for a hash or an integer for an array) >> >=20 >> > I don't think this is a good idea. I think it should rather depend on t= he class of the receiver. >> >=20 >> > {}.bury(:users, 0, :name, 'Matz') # =3D> {:users =3D> {0 =3D> {:name =3D= > "Matz"}}} >> > [].bury(:users, 0, :name, 'Matz') # =3D> error >> > {}.bury(0, 1, 2, :foo) # =3D> {0 =3D> {1 =3D> {2 =3D> :foo}}} >> > [].bury(0, 1, 2, :foo) # =3D> [[nil, [nil, nil, :foo]]] >> >=20 >> > and similar for struct. >>=20 >> I agree. I should clarify that I was assuming the class of the receiver (= `data`) was known in my example. The inference I was talking about was that a= buried `0` would imply an array position by default instead of a hash key. >>=20 >>=20 >>=20 >>=20 >> ---------------------------------------- >> Feature #11747: "bury" feature, similar to 'dig' but opposite=20 >> https://bugs.ruby-lang.org/issues/11747#change-55211 >>=20 >> * Author: damien sutevski >> * Status: Feedback >> * Priority: Normal >> * Assignee: Yukihiro Matsumoto >> ---------------------------------------- >> In Matz's recent Rubyconf talk, he used this example for the new 'dig' fe= ature coming in Ruby 2.3: >>=20 >> ~~~ruby >> # we want this >> data[:users][0][:name] >>=20 >> # we can do this w/o nil errors >> data.dig(:users, 0, :name) >> ~~~ >>=20 >> 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: >>=20 >> ~~~ruby >> data.bury(:users, 0, :name, 'Matz') >> ~~~ >>=20 >> This will create a nested hash or an array automatically at each step if i= t doesn't already exist, and that can be inferred from the what the user is p= assing (such as a symbol or string for a hash or an integer for an array). I= t's similar to autovivification but more powerful! >>=20 >> This behavior is very common, at least in my experience, so a dry method b= uilt into Ruby would be awesome!=20 >>=20 >>=20 >>=20 >> --=20 >> https://bugs.ruby-lang.org/ --Apple-Mail-A0F510F5-5696-4BEC-B513-A977A91CA918 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Remove me from mail list please <= br>
Sent from my iPhone
Billy E Brand
404-597-9615

On Dec 17, 2015, at 23:06, Joseph Jones <joeyi5216@gmail.com> wrote:

Joseph Jones liked your message with Boxer.


On Dec= ember 2, 2015 at 16:36:12 MST, dameyaw= n@gmail.com wrote:
Issue #11747 h= as been updated by damien sutevski.


Tsuyoshi Sawada wrote:
>= ; > inferred from the what the user is passing (such as a symbol or stri= ng for a hash or an integer for an array)
>
> I don't think thi= s is a good idea. I think it should rather depend on the class of the receiv= er.
>
> {}.bury(:users, 0, :name, 'Matz') # =3D> {:users= =3D> {0 =3D> {:name =3D> "Matz"}}}
> [].bury(:users, 0, := name, 'Matz') # =3D> error
> {}.bury(0, 1, 2, :foo) # =3D> {= 0 =3D> {1 =3D> {2 =3D> :foo}}}
> [].bury(0, 1, 2, :foo) #= =3D> [[nil, [nil, nil, :foo]]]
>
> and similar for struct.<= br>
I agree. I should clarify that I was assuming the class of the receiv= er (`data`) was known in my example. The inference I was talking about was t= hat a buried `0` would imply an array position by default instead of a hash= key.




----------------------------------------
Feature= #11747: "bury" feature, similar to 'dig' but opposite
https://bugs.ruby-lang.org/i= ssues/11747#change-55211

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

~~~ruby
#= we want this
data[:users][0][:name]

# we can do this w/o nil erro= rs
data.dig(:users, 0, :name)
~~~

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:

~~~ruby
data.bury(:users, 0, :name,= 'Matz')
~~~

This will create a nested hash or an array automatica= lly at each step if it doesn't already exist, and that can be inferred from t= he what the user is passing (such as a symbol or string for a hash or an int= eger for an array). It's similar to autovivification but more powerful!
<= br>This behavior is very common, at least in my experience, so a dry method b= uilt into Ruby would be awesome!



--
https://bugs.ruby-lang.org/
= --Apple-Mail-A0F510F5-5696-4BEC-B513-A977A91CA918--