ruby/ruby — pull request #275 by tylerdooling (2013-04-06 14:44:40+00:00)
Title: hash.c: Add traverse for clean nested access

Traverses the given levels of a hash and returns the value associated with the
last key provided. Allows for concise access for deeply nested hashes - often encountered with dealing with external restful apis.

hash = { level1: { level2: { level3: 3 } } }
hash.traverse :level1, :level2, :level3 #=> 3

Also supports a default value block if the value is nil or the key is
not found.

hash.traverse(:level1, :level2, :non_existent_key) { 5 } #=> 5

I'm aware that the probability of getting a feature accepted via PR is slim, but this is something I've build into my rubies for a while and use often so it seemed worth the attempt.