This blog is part of our Rails 5.2 series.

Ruby 2.3 added fetch_values method to hash.

By using fetch_values we are able to get values for multiple keys in a hash.

capitals = { usa: "Washington DC" , china: "Beijing" , india: "New Delhi" , australia: "Canberra" } capitals . fetch_values ( :usa , :india ) #=> ["Washington DC", "New Delhi"] capitals . fetch_values ( :usa , :spain ) { | country | "N/A" } #=> ["Washington DC", "N/A"]

Rails 5.2 introduces method fetch_values on HashWithIndifferentAccess. We’ll hence be able to fetch values of multiple keys on any instance of HashWithIndifferentAccess class.