Skip to content

Instantly share code, notes, and snippets.

@anaumov
Created October 8, 2018 07:40
Show Gist options
  • Save anaumov/1bdc1d1c1aa3805dc2a9eea583d61276 to your computer and use it in GitHub Desktop.
Save anaumov/1bdc1d1c1aa3805dc2a9eea583d61276 to your computer and use it in GitHub Desktop.
Ruby dig example
hash = { user: { name: 'Peter' } }
hash[:user][:name] # => "Peter"
hash = {}
hash[:user][:name] # => NoMethodError: undefined method `[]' for nil:NilClass
hash[:user] && hash[:user][:name] # => "Peter"
{ user: { name: 'Peter' } }.dig(:user, :name) # => "Peter"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment