Code Editor
Data Structures

Introduction to Hashes

We know that arrays are indexed with numbers that start with 0 and go up to the array's length minus one. (Think about it: an array with four elements has the indices 0, 1, 2, and 3.)

But what if we want to use numeric indices that don't go in order from 0 to the end of the array? What if we don't want to use numbers as indices at all? We'll need a new array structure called a hash.

Hashes are sort of like JavaScript objects or Python dictionaries. If you haven't studied those languages, all you need to know that a hash is a collection of key-value pairs. Hash syntax looks like this:

hash = { key1 => value1, key2 => value2, key3 => value3 }

Values are assigned to keys using =>. You can use any Ruby object for a key or value.


We've created a hash in the editor to the right. See how it's made up of keys and values? Check out the code below it: it should look vaguely familiar. Click Run to see what it'll do!

Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.