Created
February 18, 2013 05:16
-
-
Save carloespinosa/4975254 to your computer and use it in GitHub Desktop.
Ruby Hash Inflections
This file contains 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
Hash.class_eval do | |
def seek(*_keys_) | |
last_level = self | |
sought_value = nil | |
_keys_.each_with_index do |_key_, _idx_| | |
if last_level.is_a?(Hash) && last_level.has_key?(_key_) | |
if _idx_ + 1 == _keys_.length | |
sought_value = last_level[_key_] | |
else | |
last_level = last_level[_key_] | |
end | |
else | |
break | |
end | |
end | |
sought_value | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment