Skip to content

Instantly share code, notes, and snippets.

@carloespinosa
Created February 18, 2013 05:16
Show Gist options
  • Save carloespinosa/4975254 to your computer and use it in GitHub Desktop.
Save carloespinosa/4975254 to your computer and use it in GitHub Desktop.
Ruby Hash Inflections
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