Skip to content

Instantly share code, notes, and snippets.

@mschuerig
Created June 8, 2012 06:50
Show Gist options
  • Save mschuerig/2894061 to your computer and use it in GitHub Desktop.
Save mschuerig/2894061 to your computer and use it in GitHub Desktop.
Safely access an element in a nested hash by its path.
class Hash
# Safely access an element in a nested hash by its path.
# If any of the intervening hashes is not present, nil is returned.
def at(*path)
val = self
path.each do |k|
val = val[k]
return nil unless val
end
val
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment