Project

General

Profile

Feature #13314

dig=

Added by sawa (Tsuyoshi Sawada) over 3 years ago. Updated over 3 years ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:80150]

Description

We have Hash#dig, and when we want to assign a key-value at a deep level, it is tempting to do:

hash.dig(:key1, :key2, :key3, :key4) = "value

when we actually needed to do:

hash.dig(:key1, :key2, :key3)&.[]=(:key4, "value")

I propose a method Hash#dig=, which should be equivalent to the following:

class Hash
  def dig=(*keys, final_key, value)
    dig(*keys)&.[](final_key, value)
  end
end

Also available in: Atom PDF