Home Use dig method to get value from deeply nested key
Post
Cancel

Use dig method to get value from deeply nested key

Learn how to extracts the nested value specified by the sequence of key objects by calling dig at each step, returning nil if any intermediate step is nil.

Example data

Lets say we have following Hash to work on, this can be a rails params object or any other Hash object

1
params = { name: "John", address: { primary: { city: 'Pokhara' } } }

And we want to get the value of city from the given object, and one way to do is:

1
2
params[:address] && params[:address][:primary] && params[:address][:primary][:city]
=> "Pokhara"

we need to do that, because if lets say address or primary key is empty than we will get

1
undefined method `[]' for nil:NilClass (NoMethodError)

And ruby has a clean and easy way to extract such values by using .dig method

1
2
params.dig(:address, :primary, :city)
=> "Pokhara"

In this case, if address or primary key do not exists, or have nil as a value, than it will return nil as result without throwing error.

More Info

This post is licensed under CC BY 4.0 by the author.