{foo: {hoi: 'hei'}, bar: 2}.traverse(:foo, :hoi) # => 'hei'
Add this line to your application's Gemfile:
gem 'hash_traverse'
And then execute:
$ bundle
Or install it yourself as:
$ gem install hash_traverse
t = HashTraverse::Traverser.new(foo: {hoi: 'hei'}, bar: 2)
t.traverse(:foo) # => {hoi: 'hei'}
t.traverse(:foo, :hoi) # => 'hei'
t.traverse(:foo, :poi) # => nil
# Can customize missing behaviour
t.traverse(:foo, :poi) { :null } # => :null
t.traverse(:foo, :poi) {|*args| args } # => [[:foo], {hoi: 'hei'}]
t.traverse(:foo, :hoi, :oi) {|*args| args } # => [[:foo, :hoi], 'hei']
Use monkey patching mode with require
-ing hash_traverse/core_ext
if you like to use HashTraverse everywhere.
require 'hash_traverse/core_ext'
{foo: {hoi: 'hei'}, bar: 2}.traverse(:foo, :hoi) # => 'hei'
- Fork it ( https://github.com/esminc/hash_traverse/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request