Skip to content

Instantly share code, notes, and snippets.

@pointcom
Last active January 23, 2018 10:42
Show Gist options
  • Save pointcom/239357b9add3129a19d98eefb3568ae6 to your computer and use it in GitHub Desktop.
Save pointcom/239357b9add3129a19d98eefb3568ae6 to your computer and use it in GitHub Desktop.
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