class DeepHash

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)

Public Class Methods

new(hash) click to toggle source
# File lib/deep_hash.rb, line 11
def initialize(hash)
  @hash = hash
end

Public Instance Methods

dig(*keys) click to toggle source
# File lib/deep_hash.rb, line 15
def dig(*keys)
  keys.inject(@hash) do |location, key|
    location[key] unless location.nil?
  end
end