Other Types as Hash Values
Learn how to use any type for key-value pairs as hash values.
We'll cover the following
Different types of key and value
In the previous lesson, we figured out that hash is a set of key-value pairs, where the key is usually a symbol or string and the value is an object. We used the type integer in our examples. However, there is no limitation here, and we can use any type for keys and values, including strings, arrays, and even other hashes.
It’s the same for arrays. Array elements can be numbers, strings, arrays themselves, and even hashes. And these hashes can contain other hashes and other arrays. In other words, we can use any combination of arrays and hashes, and create a deeply nested data structure called JavaScript Object Notation (JSON). We’ve already mentioned that the hash in JavaScript is often called an object”. Even though this acronym originates in JavaScript, it is also widely used in Ruby.
Here is how a combination might look:
obj = {
soccer_ball: { weight: 410, colors: [:red, :blue] },
tennis_ball: { weight: 58, colors: [:yellow, :white] },
golf_ball: { weight: 45, colors: [:white] }
}
Let’s break down the code above into smaller parts. Here is the top-level hash:
obj = {
... this is the hash ...
}
For every key in the hash above we define another hash, like:
obj = {
soccer_ball: { ...this is the hash... },
tennis_ball: { ...this is another hash... },
golf_ball: { ...and another hash... }
}
All of these three hashes have the following keys and corresponding values:
- Key
:weight
(symbol), value type:Array
- Key
:colors
(symbol), value type:Array
Notice the :golf_ball hash, which has a :colors key with the type of Array
, but technically it can be written as follows:
golf_ball: { weight: 45, color: :white }
We’ve intentionally chosen the Array
type initially because it was our choice. Ruby allows any syntax, but we decided to keep the structure, or schema. We’re doing this for the following reasons:
-
To avoid ambiguity. Every line looks similar.
-
To keep an array of colors, where we can add another color in the future. It’s a good example of how programmers plan their programs. They can choose any type, but what works better now, and what will work better in the future? Is there any information that indicates that we might need this or another type in the future? In our case, we have this information: all previous records have more than one color.
-
To keep the code simple. We’re not talking about existing code but about the code that traverses a data structure. If the data structure is uniform, the code will be simpler, because there won’t be any special or edge cases like handling two different types for the same purpose.
In other words, JSON objects often have a schema: well-defined structures. But how can one access such a nested object? Well, the same way we access arrays and hashes, but combining these access operations.
Let’s print all the colors for the tennis ball:
arr = obj[:tennis_ball][:colors]
puts arr
Also, let’s print the weight for the golf ball:
weight = obj[:golf_ball][:weight]
puts weight
Let’s add a new :green
color to array of colors
for the tennis ball:
obj[:tennis_ball][:colors].push(:green)
The structure we defined above starts with a curly bracket. It means that the JSON object has the type of hash. But JSON objects can be arrays too. It depends on our application requirements. If the primary goal of our application is to print a list, and we don’t need any random access to update or read the hash, JSON object above can be represented the other way:
obj = [
{ type: :soccer_ball, weight: 410, colors: [:red, :blue] },
{ type: :tennis_ball, weight: 58, colors: [:yellow, :white] },
{ type: :golf_ball, weight: 45, colors: [:white] }
]
The structure above is nothing but an array of objects with some key properties:
obj = [ {}, {}, {} ]
Depending on requirements, a programmer defines the correct representation of data.
The program for the above example is given below:
Get hands-on with 1400+ tech skills courses.