Sha256: 20e4741dd3a62aa4593794eb8e13f34e2206260ec8500148c3c67de73d7dbd3f

Contents?: true

Size: 649 Bytes

Versions: 18

Compression:

Stored size: 649 Bytes

Contents

module DuckPuncher
  module Ducks
    module Hash
      # http://coryodaniel.com/index.php/2009/12/30/ruby-getting-deeply-nested-values-from-a-hash-in-one-line-of-code/
      def dig(*_keys_)
        last_level = self
        sought_value = nil
        _keys_.each_with_index do |_key_, _idx_|
          break unless last_level.respond_to?(:has_key?)
          break unless last_level.has_key?(_key_)
          if _idx_ + 1 == _keys_.length
            sought_value = last_level[_key_]
          else
            last_level = last_level[_key_]
          end
        end

        sought_value
      end unless method_defined?(:dig)
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
duck_puncher-3.0.0 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.16.0 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.15.0 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.14.1 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.14.0 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.13.0 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.12.1 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.12.0 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.11.0 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.10.2 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.10.1 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.10.0 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.9.3 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.9.2 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.9.1 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.9.0 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.8.0 lib/duck_puncher/ducks/hash.rb
duck_puncher-2.7.0 lib/duck_puncher/ducks/hash.rb