How to iterate over a Hash in Ruby? Last Updated : 27 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will discuss how to iterate over a hash in Ruby. By Iterating over a hash we can access key-value pairs and perform operations on them. Let us study different methods to iterate over a hash in ruby Table of Content Iterating over a hash using each methodIterating over a hash using each_key method:Iterating over a hash using each_value method:Iterating over a hash using each methodEach method allows us to iterate over each key-value pair in the hash and executes the provided block with two arguments: the key and the value. Syntax: hash.each { |key, value| block } Example: In this example we're printing each key-value pair in hash fruits Ruby #Ruby program to Iterate over a hash using each method: fruits = { "apple" => 5, "banana" => 3, "orange" => 6 } # Iterate over each key and print them fruits.each_key { |fruit| puts fruit } Outputapple banana orange Iterating over a hash using each_key method:The each method allows us to iterates over each key in the hash and executes the provided block with one argument: the key. Syntax: hash.each_key { |key| block } Example: In this example we're printing each key of hash fruits Ruby #Ruby program to Iterate over a hash using each_key method: fruits = { "apple" => 5, "banana" => 3, "orange" => 6 } # Iterate over each key and print them fruits.each_key { |fruit| puts fruit } Outputapple banana orange Iterating over a hash using each_value method:The each method allows us to iterates over each value in the hash and executes the provided block with one argument: the value Syntax: hash.each_value { |value| block } Example: In this example we're printing each value of hash fruits Ruby #Ruby program to Iterate over a hash using each_value method: fruits = { "apple" => 5, "banana" => 3, "orange" => 6 } # Iterate over each value and print them fruits.each_value { |quantity| puts quantity } Output5 3 6 Comment More infoAdvertise with us Next Article How to Access elements of nested hashes in Ruby? M mishraaabcf9 Follow Improve Article Tags : Ruby Similar Reads How to Parse Hash in Ruby? Parsing a hash in Ruby entails gaining access to its keys and values, iterating over them, and carrying out any necessary actions. The article focuses on discussing the ways to parse a hash in Ruby. Table of Content Iterating through a HashAccessing Hash ElementsSorting a HashIterating through a Has 2 min read How to Sort a Hash in Ruby? In this article, we will learn how to sort a Hash in ruby. We can sort the map by key, from low to high or high to low, using the sortBy method. Syntax:sorted_hash = original_hash.sort_by { |key, value| expression }.to_h Example 1: In this example, we sort a hash in ascending order Ruby # Define a h 2 min read How to Make a Custom Array of Hashes in Ruby? Prerequisites: Hashes and Arrays in Ruby Arrays and hashes are data structures that allow you to store multiple values at once. In this article, we will explore their syntax, how to combine functionalities of both to create an array of hashes, retrieve values, and loop through them. An array is a co 3 min read How to Access elements of nested hashes in Ruby? RIn this article, we will discuss how to access elements of nested hashes in Ruby. We can access elements of nested hashes through different methods ranging from using the dig method to the fetch method. Table of Content Access elements of nested hashes using square bracketsAccess elements of nested 2 min read Ruby | Hash to_a method Hash#to_a() is a Hash class method which gives a nested array of the key-value pair. Syntax: Hash.to_a() Parameter: Hash values Return: array representation of the hash Example #1 : Ruby # Ruby code for Hash.to_a() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c 2 min read How to convert Hash to Object in Ruby? In this article, we will discuss how to convert a hash to an object in Ruby. Converting hash to an object allows one to access hash keys as object attributes, providing them with a more intuitive way to work with data. Table of Content Using OpenStructUsing StructUsing Hash#eachUsing OpenStructOpenS 3 min read Like