Skip to content

Instantly share code, notes, and snippets.

@fronx
Created September 5, 2011 16:23
Show Gist options
  • Select an option

  • Save fronx/1195380 to your computer and use it in GitHub Desktop.

Select an option

Save fronx/1195380 to your computer and use it in GitHub Desktop.
def has_key_path?(h, path)
!path.detect do |key|
!(h.respond_to?(:has_key?) && h.has_key?(key) && true.tap { h = h[key] })
end
end
h = {:a => {:b => 1, :c => nil}}
puts has_key_path?(h, [:a, :x]) # false
puts has_key_path?(h, [:a, :b]) # true
puts has_key_path?(h, [:a, :c]) # true
puts has_key_path?(h, [:a, :c, :d]) # false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment