Issue #13179 has been updated by MarioRuiz (Mario Ruiz Snchez).


bettisworth (wurde _) wrote:
> I came across a scenario where I needed the ability to update a deeply nested hash (Rails i18n yaml files). This seemed like something that would exist naturally in the DSL of ruby, but I could only find dig() method which only retrieves values if they exist. 24 hours later I wrote a update_deep_hash method (I wouldn't wish this type of recursive coding on anyone else within the Ruby community).
> 
> Attached is the solution I hacked together. My question is if we can have a Hash.dig() method which reaches into a deep hash can we expand on this to include update methods of similar nature?

What I am using in my code is a simple dig method... at least for the best cases is working nice:

~~~ ruby
#my_hash.bury([:accent,2,:original],"the value to set")
class Hash
  def bury(where, value)
      me=self
      where[0..-2].each{|key|
        me=me[key]
      }
      me[where[-1]]=value
  end
end

#my_array.bury([2,1,:original],"the value to set")
class Array
  def bury(where, value)
      me=self
      where[0..-2].each{|key|
        me=me[key]
      }
      me[where[-1]]=value
  end
end

~~~


----------------------------------------
Feature #13179: Deep Hash Update Method
https://bugs.ruby-lang.org/issues/13179#change-69526

* Author: bettisworth (wurde _)
* Status: Rejected
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I came across a scenario where I needed the ability to update a deeply nested hash (Rails i18n yaml files). This seemed like something that would exist naturally in the DSL of ruby, but I could only find dig() method which only retrieves values if they exist. 24 hours later I wrote a update_deep_hash method (I wouldn't wish this type of recursive coding on anyone else within the Ruby community).

Attached is the solution I hacked together. My question is if we can have a Hash.dig() method which reaches into a deep hash can we expand on this to include update methods of similar nature?

---Files--------------------------------
update_deep_hash.rb (2.26 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>