...

/

Other Types as Hash Values

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:

 ...