intridea/hashie — pull request #134 by tylerdooling (2014-02-26 03:07:07+00:00)
Title: Adds #traverse for nested access.

## #traverse
Provides quick and concise access of deeply nested values.

For example,
```ruby
hash = { user: { location: { address: '123 Street' } } }
hash.traverse :user, :location, :address #=> '123 Street'
```
as opposed to,
```ruby
hash.fetch(:user, {}).fetch(:location, {}).fetch(:address) #=> '123 Street'
```
If a block is provided its value will be returned if the key does not exist.
```ruby
hash.traverse(:user, :non_existent_key) { 'a value' } #=> 'a value'
```
This has been particularly useful for fetching values from deeply nested API responses or params hashes.

As a result of the similarity of behavior for arrays and hashes when fetching values, and the reality that I often use this tool when dealing with APIs, it also supports arrays in the nested structure.
```ruby
hash = { user: { roles: [ 'admin', 'developer' ] } }
hash.traverse :user, :roles, 1 #=> 'developer'
```