Feature #8246


Added by zzak (Zachary Scott) about 5 years ago. Updated over 3 years ago.

Target version:


From (())

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}))

275.patch (3.05 KB) 275.patch zzak (Zachary Scott), 04/11/2013 08:03 AM

Related issues

Related to Ruby trunk - Feature #8237: Logical method chaining via inferred receiverClosed
Is duplicate of Ruby trunk - Feature #5531: deep_value for dealing with nested hashesClosed


#1 Updated by marcandre (Marc-Andre Lafortune) about 5 years ago

  • Assignee set to matz (Yukihiro Matsumoto)
  • Priority changed from Normal to 3

Marking as duplicate of #5531.
It's also related to #8237 because one could envision something like:

hash[:level1]??.[:level2]??.[:non_existent_key] || 5

#2 [ruby-core:54197] Updated by headius (Charles Nutter) about 5 years ago

As a feature that affects all Ruby implementations, this should probably move to CommonRuby:

#3 [ruby-core:65686] Updated by zzak (Zachary Scott) over 3 years ago

  • Status changed from Open to Closed
  • Priority changed from 3 to Normal

Yeah, sorry, this is a duplicate of #5531 with just a different method name

Also available in: Atom PDF