Created
October 8, 2018 07:40
-
-
Save anaumov/1bdc1d1c1aa3805dc2a9eea583d61276 to your computer and use it in GitHub Desktop.
Ruby dig example
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 = { 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