DEV Community

Code Salley
Code Salley

Posted on • Updated on

Ruby most useful Hash methods

Since almost everything in ruby is an object, you get tons of feature that makes data manipulation easier. Ruby hash is one of the data types and is easy to work with. And if you are looking to work with a key and value pair, the hash is the way to go. Here are some of the useful hash methods to know.

Hash#fetch

Fetch method allows you to retrieve a value from a hash using a key. If the key is not found in the hash, you can specify a default value to return instead of nil.

person = { "name" => "John", "age" => 30, "city" => "New York" }

person.fetch("name")
# John

# Provided default value is returned instead of nil
# when the value is not found
person.fetch("country", "Unknown")
# Unknown
Enter fullscreen mode Exit fullscreen mode

Hash#dig

dig method allows you to access nested values in a hash without having to check for the existence of each key in the chain. The dig method takes a list of keys as arguments and returns the value at the specified key path.

person = {
  name: "John",
  age: 30,
  address: {
    street: "123 Main St",
    city: "New York",
    country: "USA"
  }
}

name = person.dig(:name)
city = person.dig(:address, :city)
country = person.dig(:address, :country)
state = person.dig(:address, :state)

# Name: John
# City: New York
# Country: USA
# State: nil
Enter fullscreen mode Exit fullscreen mode

Hash#then

then method, you can chain methods together and pass the result of one method to the next method as an argument. The then method is called on the object and takes a block as an argument. The block is called with the object as the argument, and the block's return value is the result of the method chain.

even_numbers = [2, 4, 6, 8, 10]

def add_ten_to_even_numbers(numbers)
  numbers.map { |num| num + 10 }
end

add_ten_to_even_numbers(even_numbers).then { |new_num| puts new_num.inspect }
# [12, 14, 16, 18, 20]
Enter fullscreen mode Exit fullscreen mode

Hash#value?

value? method allows you to check if a hash contains a specific value. The method returns true if the value is found in the hash, and false otherwise. This takes out the complexity of trying to loop through a hash.

person = { name: "John", age: 30, city: "New York", country: "GH", code: "GH"}

hash.value?(30)
# true 

hash.value?(31)
# false

just an addon, values_at method allows you to retrieve the values of multiple keys from a hash.

hash.values_at(:age, :city, :country)
# [30, "New York", "GH"]

Enter fullscreen mode Exit fullscreen mode

Hash#tap

allows you to "tap into" a method chain, and do something with the object that is being passed along the chain. The block is called with the object as the argument and the block's return value is ignored.

valid_names = ["John", "Smith", "Jane", "Doe", "Mick", "Jagger", "Sam", "stone"]
guest_names = [ "Doe", "Jagger", "Khamil", "Paul"]

def check_guest_names(guest_names, valid_names)
  [].tap do |x|
    guest_names.each do |name|
      x << name if valid_names.include?(name)
    end
  end
end

response = check_guest_names(guest_names, valid_names)
puts response.inspect
# ["Doe", "Jagger"]

(1..10).to_a.select { |x| x.even? }.tap { |x| puts "Even #{x}" }
# Even [2, 4, 6, 8, 10]

Enter fullscreen mode Exit fullscreen mode

Hope it was helpful, and beware of the bang(!)

Top comments (1)

Collapse
 
lucianghinda profile image
Lucian Ghinda

Nice article exploring Hash methods.

I like quite a lot then and because that is a method defined on Kernel it should be available on almost *all objects in Ruby