How to Create a Map of Maps in Ruby - Step by Step Examples
How to Create a Map of Maps in Ruby ?
Answer
To create a map of maps in Ruby, you can use the Hash data structure. A map of maps allows you to organize data hierarchically, where each key in the outer hash points to another hash.
✐ Examples
1 Creating a Map of Maps with Integer Keys and Values
We can create a map of maps in Ruby by using the Hash data structure. This example demonstrates creating a hash where both the outer and inner hashes use integers for keys and values.
For example,
- We start by declaring and initializing a hash named
outer_hash
where each value is another hash. - We populate the inner hashes with key-value pairs.
- We insert the inner hashes into the outer hash using appropriate keys.
- We print the resulting hash of hashes to the console to verify the structure.
Ruby Program
outer_hash = {}
# Populate the inner hashes
inner_hash1 = {1 => 10, 2 => 20, 3 => 30}
inner_hash2 = {4 => 40, 5 => 50, 6 => 60}
# Insert the inner hashes into the outer hash
outer_hash[1] = inner_hash1
outer_hash[2] = inner_hash2
# Print the resulting hash of hashes
puts 'Hash of hashes with integer keys and values:'
outer_hash.each do |outer_key, inner_hash|
puts "Outer key: #{outer_key}"
inner_hash.each do |inner_key, value|
puts " Inner key: #{inner_key}, value: #{value}"
end
end
Output
Hash of hashes with integer keys and values: Outer key: 1 Inner key: 1, value: 10 Inner key: 2, value: 20 Inner key: 3, value: 30 Outer key: 2 Inner key: 4, value: 40 Inner key: 5, value: 50 Inner key: 6, value: 60
2 Creating a Map of Maps with Mixed Key and Value Types
We can create a map of maps in Ruby by using the Hash data structure. This example demonstrates creating a hash where the outer hash uses strings for keys and the inner hashes use integers for keys and strings for values.
For example,
- We start by declaring and initializing a hash named
outer_hash
where each value is another hash with mixed key and value types. - We populate the inner hashes with key-value pairs.
- We insert the inner hashes into the outer hash using appropriate keys.
- We print the resulting hash of hashes to the console to verify the structure.
Ruby Program
outer_hash = {}
# Populate the inner hashes
inner_hash1 = {1 => 'one', 2 => 'two', 3 => 'three'}
inner_hash2 = {4 => 'four', 5 => 'five', 6 => 'six'}
# Insert the inner hashes into the outer hash
outer_hash['first'] = inner_hash1
outer_hash['second'] = inner_hash2
# Print the resulting hash of hashes
puts 'Hash of hashes with mixed key and value types:'
outer_hash.each do |outer_key, inner_hash|
puts "Outer key: #{outer_key}"
inner_hash.each do |inner_key, value|
puts " Inner key: #{inner_key}, value: #{value}"
end
end
Output
Hash of hashes with mixed key and value types: Outer key: first Inner key: 1, value: one Inner key: 2, value: two Inner key: 3, value: three Outer key: second Inner key: 4, value: four Inner key: 5, value: five Inner key: 6, value: six
3 Creating a Map of Maps with String Keys and Double Values
We can create a map of maps in Ruby by using the Hash data structure. This example demonstrates creating a hash where both the outer and inner hashes use strings for keys and floats for values.
For example,
- We start by declaring and initializing a hash named
outer_hash
where each value is another hash with strings for keys and floats for values. - We populate the inner hashes with key-value pairs.
- We insert the inner hashes into the outer hash using appropriate keys.
- We print the resulting hash of hashes to the console to verify the structure.
Ruby Program
outer_hash = {}
# Populate the inner hashes
inner_hash1 = {'A' => 1.1, 'B' => 2.2, 'C' => 3.3}
inner_hash2 = {'D' => 4.4, 'E' => 5.5, 'F' => 6.6}
# Insert the inner hashes into the outer hash
outer_hash['group1'] = inner_hash1
outer_hash['group2'] = inner_hash2
# Print the resulting hash of hashes
puts 'Hash of hashes with string keys and float values:'
outer_hash.each do |outer_key, inner_hash|
puts "Outer key: #{outer_key}"
inner_hash.each do |inner_key, value|
puts " Inner key: #{inner_key}, value: #{value}"
end
end
Output
Hash of hashes with string keys and float values: Outer key: group1 Inner key: A, value: 1.1 Inner key: B, value: 2.2 Inner key: C, value: 3.3 Outer key: group2 Inner key: D, value: 4.4 Inner key: E, value: 5.5 Inner key: F, value: 6.6
Summary
In this tutorial, we learned How to Create a Map of Maps in Ruby language with well detailed examples.
More Ruby Maps Tutorials
- How to create an Empty Map in Ruby ?
- How to create a Map with Initial Key-Value Pairs in Ruby ?
- How to Print a Map in Ruby ?
- How to Add a Key-Value Pair to a Map in Ruby ?
- How to Set a Default Value for a Key in a Map in Ruby ?
- How to Update the Value for a Key in a Map in Ruby ?
- How to Check if a Map is Empty in Ruby ?
- How to Check if a Key Exists in a Map in Ruby ?
- How to Check if a Value Exists in a Map in Ruby ?
- How to Get the Value Associated with a Key in a Map in Ruby ?
- How to Remove a Key-Value Pair from a Map in Ruby ?
- How to Remove Key-Value Pairs from a Map Based on Values in Ruby ?
- How to Clear All Key-Value Pairs from a Map in Ruby ?
- How to Iterate Over Keys in a Map in Ruby ?
- How to Iterate Over Values in a Map in Ruby ?
- How to Iterate Over Entries (Key-Value Pairs) in a Map in Ruby ?
- How to Get the Size (Number of Key-Value Pairs) of a Map in Ruby ?
- How to Convert a Map to an Array of Keys in Ruby ?
- How to Convert a Map to an Array of Values in Ruby ?
- How to Convert a Map to an Array of Key-Value Pairs in Ruby ?
- How to Merge Two Maps in Ruby ?
- How to Copy a Map in Ruby ?
- How to Check if Two Maps are Equal in Ruby ?
- How to Sort a Map by Keys in Ruby ?
- How to Sort a Map by Values in Ruby ?
- How to Filter a Map Based on Keys in Ruby ?
- How to Filter a Map Based on Values in Ruby ?
- How to Reduce Values in a Map to a Single Value in Ruby ?
- How to Convert an Array of Key-Value Pairs to a Map in Ruby ?
- How to Convert a Map to a JSON String in Ruby ?
- How to Convert a JSON String to a Map in Ruby ?
- How to Swap Keys and Values in a Map in Ruby ?
- How to Create a Map of Maps in Ruby ?
- How to Iterate Over a Map of Maps in Ruby ?