How to convert Array to Hash in Ruby?
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.
Table of Content
- Converting array to hash using Hash::[] constructor
- Converting array to hash using Array#each_slice
- Converting array to hash using Hash#[] with a block
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
# 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::[]
# 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.
# 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}