Project

General

Profile

Actions

Feature #11747

closed

"bury" feature, similar to 'dig' but opposite

Added by dam13n (damien sutevski) over 8 years ago. Updated almost 3 years ago.

Status:
Rejected
Target version:
-
[ruby-core:71709]

Description

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!


Files

bury_examples.rb (1 KB) bury_examples.rb briankung (Brian Kung), 05/17/2018 04:36 PM

Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #19699: Need a way to store values like digClosedActions
Has duplicate Ruby master - Feature #13179: Deep Hash Update MethodRejectedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0