How to convert Array to Hash in Ruby?
Last Updated :
27 Mar, 2024
In this article, we will discuss how to convert an array to a hash in Ruby. Converting an array to a hash can be useful when we have data in an array format and need to organize it into key-value pairs for easier access and manipulation.
Converting an array to hash using Hash::[] constructor
The Hash::[] constructor method creates a hash directly from an array where each pair of elements in the array is treated as a key-value pair.
Syntax:
Hash[*array]
Example: In this example,we convert an array into a hash , where each element in the array is transformed into a key-value pair using Hash::[] constructor
Ruby
# Define an array with elements representing key-value pairs
array = [:a, 1, :b, 2, :c, 3]
# Convert an array into a hash Using Hash::[] constructor
# Convert the array directly into a hash using the Hash::[] constructor
hash = Hash[*array]
# Output the resulting hash
puts hash.inspect # Output: {:a=>1, :b=>2, :c=>3}
Output{:a=>1, :b=>2, :c=>3}
Converting array to hash using Array#each_slice
Array#each_slice method allows to iterate over the array in chunks where each chunk contains two elements representing a key-value pair.Then, using the Hash::[]
constructor, we can convert these pairs into a hash.
Syntax:
Hash[*array.each_slice(2).to_a.flatten]
Example: In this example we use Array#each_slice to Iterate over the array in pairs , then flatten the resulting arrays and convert them into a hash using Hash::[]
Ruby
# Program in ruby to convert an array into a hash using Array#each_slice and Hash::[] constructor
# Define an array with elements representing key-value pairs
array = [:a, 1, :b, 2, :c, 3]
# Iterate over the array in pairs using each_slice(2), then flatten the resulting arrays
# and convert them into a hash using Hash::[]
hash = Hash[*array.each_slice(2).to_a.flatten]
# Output the resulting hash
puts hash.inspect # Output: {:a=>1, :b=>2, :c=>3}
Output{:a=>1, :b=>2, :c=>3}
Converting array to hash using Hash#[] with a block
In this method we iterate over the array and processes each element individually. Then we can specify a block where you define how each element should be converted into a key-value pair.
Syntax:
array.each_with_object({}) { |element, hash| hash[element_key] = element_value }
Example: In this example we iterates over the array in pairs using each_slice(2)
and assigns each pair's elements as key-value pairs in a new hash.
Ruby
# Define an array with elements representing key-value pairs
array = [:a, 1, :b, 2, :c, 3]
# convert an array into a hash Using Hash#[] with a block
# Iterate over the array in pairs using each_slice(2)
# For each pair, assign the first element as the key and the second element as the value
hash = array.each_slice(2).each_with_object({}) { |(key, value), h| h[key] = value }
# Output the resulting hash
puts hash.inspect # Output: {:a=>1, :b=>2, :c=>3}
Output{:a=>1, :b=>2, :c=>3}
Similar Reads
How to Convert Hash to JSON in Ruby? Ruby Hash is an unordered set of data in key-value pairs. These are mutable and can store multiple data types. JSON stands for Javascript Object Notation and is a human-readable file format that is commonly used in web services and API calls. In this article, we will discuss how to convert a hash to
2 min read
How to Convert JSON to Hash in Ruby? In this article, we will discuss how to convert JSON to hash in Ruby. We can convert JSON to hash through different methods ranging from parsing JSON and JSON.load to using YAML.safe_load Method. Table of Content Using JSON.parse MethodUsing JSON.load MethodUsing YAML.safe_load Method Using JSON.par
2 min read
How to convert String to Hash in Ruby? In this article, we will discuss how to convert string to hash in ruby. We can convert a string to a hash through different methods ranging from parsing JSON and parsing YAML to custom string parsing. Table of Content Converting string to hash using JSON parsingConverting string to hash using YAML p
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
How To Convert Data Types in Ruby? Data type conversion is a frequent job in Ruby, particularly when working with various data types or integrating with other systems. Ruby offers a wide range of operators and methods for fluid data type conversion. Effective Ruby programming requires an understanding of these conversion methods. The
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