* [ruby-core:71709] [Ruby trunk - Feature #11747] [Open] "bury" feature, similar to 'dig' but opposite
[not found] <redmine.issue-11747.20151127201637@ruby-lang.org>
@ 2015-11-27 20:16 ` dameyawn
2015-11-27 22:33 ` [ruby-core:71714] [Ruby trunk - Feature #11747] [Feedback] " nobu
` (7 subsequent siblings)
8 siblings, 0 replies; 9+ messages in thread
From: dameyawn @ 2015-11-27 20:16 UTC (permalink / raw)
To: ruby-core
Issue #11747 has been reported by damien sutevski.
----------------------------------------
Feature #11747: "bury" feature, similar to 'dig' but opposite
https://bugs.ruby-lang.org/issues/11747
* Author: damien sutevski
* Status: Open
* 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:
\# we want this
data[:users][0][:name]
\# we can do this w/o nil errors
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:
data.bury(:users, 0, :name, 'Matz')
This will create a nested hash or an array automatically at each step if it doesn't already exist, and that can be inferred from the what the user is passing (such as a symbol or string for a hash or an integer for an array). It's similar to autovivification but more powerful!
This behavior is very common, at least in my experience, so a dry method built into Ruby would be awesome!
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 9+ messages in thread
* [ruby-core:71714] [Ruby trunk - Feature #11747] [Feedback] "bury" feature, similar to 'dig' but opposite
[not found] <redmine.issue-11747.20151127201637@ruby-lang.org>
2015-11-27 20:16 ` [ruby-core:71709] [Ruby trunk - Feature #11747] [Open] "bury" feature, similar to 'dig' but opposite dameyawn
@ 2015-11-27 22:33 ` nobu
2015-11-28 14:38 ` [ruby-core:71720] [Ruby trunk - Feature #11747] " sawadatsuyoshi
` (6 subsequent siblings)
8 siblings, 0 replies; 9+ messages in thread
From: nobu @ 2015-11-27 22:33 UTC (permalink / raw)
To: ruby-core
Issue #11747 has been updated by Nobuyoshi Nakada.
Description updated
Status changed from Open to Feedback
How can it know what should be created, hash, array, or struct?
----------------------------------------
Feature #11747: "bury" feature, similar to 'dig' but opposite
https://bugs.ruby-lang.org/issues/11747#change-55120
* Author: damien sutevski
* Status: Feedback
* 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:
~~~ruby
# we want this
data[:users][0][:name]
# we can do this w/o nil errors
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 automatically at each step if it doesn't already exist, and that can be inferred from the what the user is passing (such as a symbol or string for a hash or an integer for an array). It's similar to autovivification but more powerful!
This behavior is very common, at least in my experience, so a dry method built into Ruby would be awesome!
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 9+ messages in thread
* [ruby-core:71720] [Ruby trunk - Feature #11747] "bury" feature, similar to 'dig' but opposite
[not found] <redmine.issue-11747.20151127201637@ruby-lang.org>
2015-11-27 20:16 ` [ruby-core:71709] [Ruby trunk - Feature #11747] [Open] "bury" feature, similar to 'dig' but opposite dameyawn
2015-11-27 22:33 ` [ruby-core:71714] [Ruby trunk - Feature #11747] [Feedback] " nobu
@ 2015-11-28 14:38 ` sawadatsuyoshi
2015-12-02 23:36 ` [ruby-core:71805] " dameyawn
` (5 subsequent siblings)
8 siblings, 0 replies; 9+ messages in thread
From: sawadatsuyoshi @ 2015-11-28 14:38 UTC (permalink / raw)
To: ruby-core
Issue #11747 has been updated by Tsuyoshi Sawada.
> inferred 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.
{}.bury(:users, 0, :name, 'Matz') # => {:users => {0 => {:name => "Matz"}}}
[].bury(:users, 0, :name, 'Matz') # => error
{}.bury(0, 1, 2, :foo) # => {0 => {1 => {2 => :foo}}}
[].bury(0, 1, 2, :foo) # => [[nil, [nil, nil, :foo]]]
and similar for struct.
----------------------------------------
Feature #11747: "bury" feature, similar to 'dig' but opposite
https://bugs.ruby-lang.org/issues/11747#change-55126
* Author: damien sutevski
* Status: Feedback
* 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:
~~~ruby
# we want this
data[:users][0][:name]
# we can do this w/o nil errors
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 automatically at each step if it doesn't already exist, and that can be inferred from the what the user is passing (such as a symbol or string for a hash or an integer for an array). It's similar to autovivification but more powerful!
This behavior is very common, at least in my experience, so a dry method built into Ruby would be awesome!
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 9+ messages in thread
* [ruby-core:71805] [Ruby trunk - Feature #11747] "bury" feature, similar to 'dig' but opposite
[not found] <redmine.issue-11747.20151127201637@ruby-lang.org>
` (2 preceding siblings ...)
2015-11-28 14:38 ` [ruby-core:71720] [Ruby trunk - Feature #11747] " sawadatsuyoshi
@ 2015-12-02 23:36 ` dameyawn
2015-12-07 7:29 ` [ruby-core:71885] [Ruby trunk - Feature #11747] [Rejected] " matz
` (4 subsequent siblings)
8 siblings, 0 replies; 9+ messages in thread
From: dameyawn @ 2015-12-02 23:36 UTC (permalink / raw)
To: ruby-core
Issue #11747 has been updated by damien 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 array)
>
> I don't think this is a good idea. I think it should rather depend on the class of the receiver.
>
> {}.bury(:users, 0, :name, 'Matz') # => {:users => {0 => {:name => "Matz"}}}
> [].bury(:users, 0, :name, 'Matz') # => error
> {}.bury(0, 1, 2, :foo) # => {0 => {1 => {2 => :foo}}}
> [].bury(0, 1, 2, :foo) # => [[nil, [nil, nil, :foo]]]
>
> and similar for struct.
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.
----------------------------------------
Feature #11747: "bury" feature, similar to 'dig' but opposite
https://bugs.ruby-lang.org/issues/11747#change-55211
* Author: damien sutevski
* Status: Feedback
* 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:
~~~ruby
# we want this
data[:users][0][:name]
# we can do this w/o nil errors
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 automatically at each step if it doesn't already exist, and that can be inferred from the what the user is passing (such as a symbol or string for a hash or an integer for an array). It's similar to autovivification but more powerful!
This behavior is very common, at least in my experience, so a dry method built into Ruby would be awesome!
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 9+ messages in thread
* [ruby-core:71885] [Ruby trunk - Feature #11747] [Rejected] "bury" feature, similar to 'dig' but opposite
[not found] <redmine.issue-11747.20151127201637@ruby-lang.org>
` (3 preceding siblings ...)
2015-12-02 23:36 ` [ruby-core:71805] " dameyawn
@ 2015-12-07 7:29 ` matz
2018-05-17 16:42 ` [ruby-core:87143] [Ruby trunk Feature#11747] " brianhingyenkung
` (3 subsequent siblings)
8 siblings, 0 replies; 9+ messages in thread
From: matz @ 2015-12-07 7:29 UTC (permalink / raw)
To: ruby-core
Issue #11747 has been updated by Yukihiro Matsumoto.
Status changed from Feedback to Rejected
It's not clear to generate either Hash, Array, or Struct (or whatever) to bury a value.
So it's better to reject now.
Matz.
----------------------------------------
Feature #11747: "bury" feature, similar to 'dig' but opposite
https://bugs.ruby-lang.org/issues/11747#change-55291
* Author: damien sutevski
* Status: Rejected
* 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:
~~~ruby
# we want this
data[:users][0][:name]
# we can do this w/o nil errors
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 automatically at each step if it doesn't already exist, and that can be inferred from the what the user is passing (such as a symbol or string for a hash or an integer for an array). It's similar to autovivification but more powerful!
This behavior is very common, at least in my experience, so a dry method built into Ruby would be awesome!
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 9+ messages in thread
* [ruby-core:87143] [Ruby trunk Feature#11747] "bury" feature, similar to 'dig' but opposite
[not found] <redmine.issue-11747.20151127201637@ruby-lang.org>
` (4 preceding siblings ...)
2015-12-07 7:29 ` [ruby-core:71885] [Ruby trunk - Feature #11747] [Rejected] " matz
@ 2018-05-17 16:42 ` brianhingyenkung
2019-07-14 2:52 ` [ruby-core:93743] [Ruby master " briank
` (2 subsequent siblings)
8 siblings, 0 replies; 9+ messages in thread
From: brianhingyenkung @ 2018-05-17 16:42 UTC (permalink / raw)
To: ruby-core
Issue #11747 has been updated by briankung (Brian Kung).
File bury_examples.rb added
matz (Yukihiro Matsumoto) wrote:
> It's not clear to generate either Hash, Array, or Struct (or whatever) to bury a value.
Would it be desirable to specify the type in a block? That would make it somewhat symmetrical to how `Hash.new` [takes a block][0] as a default value. For example:
[{users: ['skipped']].bury(:users, 1, :name, 'Matz') { Hash.new }
# [{:users => ['skipped', {:name => 'Matz'}]}]
{users: {0 => nil}}.bury(:users, 0, :name, 'Matz') { |next_arg| Struct.new(next_arg).new }
# {:users => {0 => #<struct name="Matz">}}
#
# If the one of the retrieved values is nil, in this case {0 => nil},
# should #bury overwrite it?
If this is okay, then it might even be nice if #dig took a block as well as a fallback value:
[].dig(1) { 'default' }
#=> "default"
Additional #bury examples have been attached as `bury_examples.rb`.
[0]: https://ruby-doc.org/core-2.5.1/Hash.html#method-c-new
----------------------------------------
Feature #11747: "bury" feature, similar to 'dig' but opposite
https://bugs.ruby-lang.org/issues/11747#change-72134
* Author: dam13n (damien sutevski)
* Status: Rejected
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version:
----------------------------------------
In Matz's recent Rubyconf talk, he used this 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 errors
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 automatically at each step if it doesn't already exist, and that can be inferred from the what the user is passing (such as a symbol or string for a hash or an integer for an array). It's similar to autovivification but more powerful!
This behavior is very common, at least in my experience, so a dry method built into Ruby would be awesome!
---Files--------------------------------
bury_examples.rb (1 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 9+ messages in thread
* [ruby-core:93743] [Ruby master Feature#11747] "bury" feature, similar to 'dig' but opposite
[not found] <redmine.issue-11747.20151127201637@ruby-lang.org>
` (5 preceding siblings ...)
2018-05-17 16:42 ` [ruby-core:87143] [Ruby trunk Feature#11747] " brianhingyenkung
@ 2019-07-14 2:52 ` briank
2020-01-11 20:38 ` [ruby-core:96790] " from-ruby-lang
2020-01-11 20:44 ` [ruby-core:96791] " from-ruby-lang
8 siblings, 0 replies; 9+ messages in thread
From: briank @ 2019-07-14 2:52 UTC (permalink / raw)
To: ruby-core
Issue #11747 has been updated by bkatzung (Brian Katzung).
Much of this has been available through my XKeys gem since Q2 2014.
```
data = {}.extend XKeys::Auto # Vs ::Hash, uses arrays for int keys
data[:users, 0, :name] # nil
data[:users, 0, :name, :raise => true] # KeyError
data[:users, :[], :name] = 'Matz' # :[] is next index, 0 in this case
# {:user=>[{:name=>"Matz"}]}
pick = [:users, 0, :name]
data[*pick] # Matz
data[:users, 0, :accesses, :else => 0] += 1
# {:user=>[{:name=>"Matz", :accesses=>1}]}
```
----------------------------------------
Feature #11747: "bury" feature, similar to 'dig' but opposite
https://bugs.ruby-lang.org/issues/11747#change-79380
* Author: dam13n (damien sutevski)
* Status: Rejected
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version:
----------------------------------------
In Matz's recent Rubyconf talk, he used this 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 errors
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 automatically at each step if it doesn't already exist, and that can be inferred from the what the user is passing (such as a symbol or string for a hash or an integer for an array). It's similar to autovivification but more powerful!
This behavior is very common, at least in my experience, so a dry method built into Ruby would be awesome!
---Files--------------------------------
bury_examples.rb (1 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 9+ messages in thread
* [ruby-core:96790] [Ruby master Feature#11747] "bury" feature, similar to 'dig' but opposite
[not found] <redmine.issue-11747.20151127201637@ruby-lang.org>
` (6 preceding siblings ...)
2019-07-14 2:52 ` [ruby-core:93743] [Ruby master " briank
@ 2020-01-11 20:38 ` from-ruby-lang
2020-01-11 20:44 ` [ruby-core:96791] " from-ruby-lang
8 siblings, 0 replies; 9+ messages in thread
From: from-ruby-lang @ 2020-01-11 20:38 UTC (permalink / raw)
To: ruby-core
Issue #11747 has been updated by cvss (Kirill Vechera).
A proposal to specify the path for `bury` with classes as values of a hash arg:
```
{}.bury(users: Array, 0: Hash, name: Hash, something: 'Value') # {user: [{name: {something: 'Value'}]}
----------------------------------------
Feature #11747: "bury" feature, similar to 'dig' but opposite
https://bugs.ruby-lang.org/issues/11747#change-83784
* Author: dam13n (damien sutevski)
* Status: Rejected
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version:
----------------------------------------
In Matz's recent Rubyconf talk, he used this 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 errors
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 automatically at each step if it doesn't already exist, and that can be inferred from the what the user is passing (such as a symbol or string for a hash or an integer for an array). It's similar to autovivification but more powerful!
This behavior is very common, at least in my experience, so a dry method built into Ruby would be awesome!
---Files--------------------------------
bury_examples.rb (1 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 9+ messages in thread
* [ruby-core:96791] [Ruby master Feature#11747] "bury" feature, similar to 'dig' but opposite
[not found] <redmine.issue-11747.20151127201637@ruby-lang.org>
` (7 preceding siblings ...)
2020-01-11 20:38 ` [ruby-core:96790] " from-ruby-lang
@ 2020-01-11 20:44 ` from-ruby-lang
8 siblings, 0 replies; 9+ messages in thread
From: from-ruby-lang @ 2020-01-11 20:44 UTC (permalink / raw)
To: ruby-core
Issue #11747 has been updated by cvss (Kirill Vechera).
A one-liner alternative for hash-only cases can be implemented using `Enumerable#reduce`:
```
root = {}
[:a, :b, :c].reduce(root){@1[@2]||={}}[:d] = 'E' # root => {:a=>{:b=>{:c=>{:d=>"E"}}}}
```
----------------------------------------
Feature #11747: "bury" feature, similar to 'dig' but opposite
https://bugs.ruby-lang.org/issues/11747#change-83785
* Author: dam13n (damien sutevski)
* Status: Rejected
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version:
----------------------------------------
In Matz's recent Rubyconf talk, he used this 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 errors
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 automatically at each step if it doesn't already exist, and that can be inferred from the what the user is passing (such as a symbol or string for a hash or an integer for an array). It's similar to autovivification but more powerful!
This behavior is very common, at least in my experience, so a dry method built into Ruby would be awesome!
---Files--------------------------------
bury_examples.rb (1 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2020-01-11 20:45 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <redmine.issue-11747.20151127201637@ruby-lang.org>
2015-11-27 20:16 ` [ruby-core:71709] [Ruby trunk - Feature #11747] [Open] "bury" feature, similar to 'dig' but opposite dameyawn
2015-11-27 22:33 ` [ruby-core:71714] [Ruby trunk - Feature #11747] [Feedback] " nobu
2015-11-28 14:38 ` [ruby-core:71720] [Ruby trunk - Feature #11747] " sawadatsuyoshi
2015-12-02 23:36 ` [ruby-core:71805] " dameyawn
2015-12-07 7:29 ` [ruby-core:71885] [Ruby trunk - Feature #11747] [Rejected] " matz
2018-05-17 16:42 ` [ruby-core:87143] [Ruby trunk Feature#11747] " brianhingyenkung
2019-07-14 2:52 ` [ruby-core:93743] [Ruby master " briank
2020-01-11 20:38 ` [ruby-core:96790] " from-ruby-lang
2020-01-11 20:44 ` [ruby-core:96791] " from-ruby-lang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).