Project

General

Profile

Actions

Feature #19699

closed

Need a way to store values like dig

Added by edufcarvalho (Eduardo Carvalho) over 1 year ago. Updated over 1 year ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:113707]

Description

Currently, there is no way to perform a .store operation on nested hashes in Ruby as there is for accessing them using .dig, so I would like to propose something like:

Hash#dig_store(*args, **kwargs, arg = nil), which fills the value at *args with **kwargs if the value at *args is a hash, or with arg if the value is something else. Here are some examples:

a = {}
a.dig_store(:a, :b, :c, :d, 10)
=> {:a=>{:b=>{:c=>{:d=>10}}}}
a = {}
a.dig_store(:a, :b, :c, :d, a: 10,  b: 20, c: 30)
=> {:a=>{:b=>{:c=>{:d=>{:a=>10, :b=>20, :c=>30}}}}}
a = {a: {b: {c: "test"}}}
a.dig_store(:a, :b, :c,  "123")
=> {:a=>{:b=>{:c=>"123"}}}
a = {a: {b: {c:  {}}}
a.dig_store(:a, :b, :c,  a: 10, b: 20)
=> {:a=>{:b=>{:c=>{:a=>10, :b=>20}}}}

I'm not that skilled on Ruby (actually a Junior developer), but i'm looking forward to implementing it if the core team approves it


Related issues 3 (1 open2 closed)

Related to Ruby master - Feature #14564: `dig` opposite methodOpenActions
Related to Ruby master - Feature #13179: Deep Hash Update MethodRejectedActions
Related to Ruby master - Feature #11747: "bury" feature, similar to 'dig' but opposite Rejectedmatz (Yukihiro Matsumoto)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0