Last active
January 23, 2018 10:42
-
-
Save pointcom/239357b9add3129a19d98eefb3568ae6 to your computer and use it in GitHub Desktop.
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
client = {client: { firstname: "bobby", lastname: "brown"}} | |
# Using the the [] getter | |
firstname = client[:client][:firstname] | |
=> "bobby" | |
# Using the new dig method, it gives you the | |
# same result the best is coming | |
firstname = client.dig(:client, :firstname) | |
=> "bobby" | |
# Let's see what we got when the leading key doesn't exist | |
firstname = client[:customer][:firstname] | |
# Traceback (most recent call last): | |
# 2: from /usr/local/bin/irb:11:in `<main>' | |
# 1: from (irb):45 | |
# NoMethodError (undefined method `[]' for nil:NilClass) | |
# And with the dig method what you got | |
firstname = client.dig(:customer, :firstname) | |
=> nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment