0.0
No commit activity in last 3 years
No release in over 3 years
Explicit backporting of Array#dig and Hash#dig for Ruby < 2.3.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.7
~> 10.0
 Project Readme

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.

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