This gem backports Array#dig and Hash#dig methods from Ruby 2.3+ to earlier versions of Ruby, only if you explicitly call it.
vs RubyDig
- RubyDig has side-effects, but RespondToDig doesn't.
- RespondToDig only supports
Array
andHash
, but RespondToDig supports anyEnumerable
objects which have[]
method such asStruct
.
Add this line to your application's Gemfile
:
gem 'respond_to_dig'
And then execute:
$ bundle
Or install it yourself as:
$ gem install respond_to_dig
require 'respond_to_dig'
h = RespondToDig.invoke({ foo: {bar: {baz: 1 }}})
h.dig(:foo, :bar, :baz) #=> 1
h.dig(:foo, :zot, :xyz) #=> nil
h.dig(:foo, :bar, :baz, :xyz) #=> TypeError
g = RespondToDig.invoke({ foo: [10, 11, 12] })
g.dig(:foo, 1) #=> 11
For more details, refer to Module: RespondToDig — Documentation.