Search⌘ K
AI Features

Other Types as Hash Values

Explore how to work with Ruby hashes that hold diverse data types, including nested hashes and arrays. Understand how to access and modify deeply nested data and learn best practices for organizing consistent and scalable hash structures.

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:

 ...