Exercise 39. Hashes, Oh Lovely Hashes

Image

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 ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.