Exercise 39. Hashes, Oh Lovely Hashes

Now I have to hurt you with another container you can use, because once you learn this container a massive world of ultra-cool will be yours. It is the most useful container ever: the dictionary.

Ruby calls them “hashes”, which is short for “hashmaps.” Other languages call them “dictionaries.” I tend to use both names, but it doesn’t matter. What does matter is what hashs do when compared to arrays. You see, an array lets you do this:

Exercise 39 Ruby Session

?> things = ['a', 'b', 'c', 'd'] => ["a", "b", "c", "d"] >> puts things[1] b => nil >> things[1] = 'z' => "z" >> puts things[1] z => nil >> things => ["a", "z", "c", "d"]

You can use numbers to “index” into a array, meaning ...

Get Learn Ruby the Hard Way: A Simple and Idiomatic Introduction to the Imaginative World of Computational Thinking with Code, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.