Ruby | dig and fetch

Ruby | dig and fetch

Vishnuprasad K's photo
Vishnuprasad K

Published on Sep 12, 2020

1 min read

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

 
Share this