Created
September 5, 2011 16:23
-
-
Save fronx/1195380 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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