[#86787] [Ruby trunk Feature#14723] [WIP] sleepy GC — ko1@...

Issue #14723 has been updated by ko1 (Koichi Sasada).

13 messages 2018/05/01
[#86790] Re: [Ruby trunk Feature#14723] [WIP] sleepy GC — Eric Wong <normalperson@...> 2018/05/01

ko1@atdot.net wrote:

[#87095] [Ruby trunk Feature#14767] [PATCH] gc.c: use monotonic counters for objspace_malloc_increase — ko1@...

Issue #14767 has been updated by ko1 (Koichi Sasada).

9 messages 2018/05/17

[ruby-core:87143] [Ruby trunk Feature#11747] "bury" feature, similar to 'dig' but opposite

From: brianhingyenkung@...
Date: 2018-05-17 16:42:39 UTC
List: ruby-core #87143
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/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next