Skip to content

Instantly share code, notes, and snippets.

@burlesona
Created February 27, 2013 22:38
Show Gist options
  • Save burlesona/5052509 to your computer and use it in GitHub Desktop.
Save burlesona/5052509 to your computer and use it in GitHub Desktop.
Hash#seek. For getting values out of deeply nested hashes.
# Use like so:
# @key_i_need = @event.seek :coordinator, :api_license, :key
class Hash
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