Class: DeepHash

Inherits:
Object
  • Object
show all
Defined in:
lib/deep_hash.rb

Overview

This class is useful for checking for keys within nested hashes

Turns

params[:foo] && params[:foo][:bar] && params[:foo][:bar][:baz]

into

DeepHash.new(params).dig(:foo, :bar, :baz)

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ DeepHash

Returns a new instance of DeepHash.



11
12
13
# File 'lib/deep_hash.rb', line 11

def initialize(hash)
  @hash = hash
end

Instance Method Details

#dig(*keys) ⇒ Object



15
16
17
18
19
# File 'lib/deep_hash.rb', line 15

def dig(*keys)
  keys.inject(@hash) do |location, key|
    location[key] unless location.nil?
  end
end