respond_to_dig

Explicit backporting of Array#dig and Hash#dig for Ruby < 2.3.


License
MIT
Install
gem install respond_to_dig -v 1.2.0

Documentation

RespondToDig

Build Status Gem Version

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 and Hash, but RespondToDig supports any Enumerable objects which have [] method such as Struct.

Installation

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

Usage

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 the details, refer Module: RespondToDig — Documentation.