Ruby | dig and fetch

dig and fetch are two powerful methods for manipulating a Hash object in Ruby.

dig

Pass the comma separated keys in the order of the nesting to dig the hash and obtain the value.

ruby_dig.png

fetch

This method fetches value for key from the hash provided.

ruby_fetch.png

fetch might raise KeyError if the key doesn't exist and you don't pass a default value.

ruby_fetch_err.png

Ahammed Nafih's photo

Learned something new! Keep up the good work :)

Sabarish Rajamohan's photo

Always had a confusion on which is better, now I have clarity, Thanks Vishnu!💎

Vishnuprasad K's photo

Happy Coding...