Arrays & Hashes

Why the lucky caterpillar dictionary?

December 7, 2014

Swallowtail

In his Poigant Guide to Ruby , 'Why the Lucky Stiff' explains the difference between arrays and hashes; it's hard to forget cuz it's a little sad. He says that the way to remember a hash is the little set of braces '{ }' looks like a dictionary. There it is sitting on a huge lectern in the library about to fall open to your favorite word. So far so good. Python also thinks of hashes as dictionaries. And now for our Array caterpillar. The way to remember an array's pair of '[ ]' is that they are like staples in sections of a caterpillar wriggling head to tail with commas for legs and each section has to go in order.

Just for sunshine's sake, let's raise our Array square braces to at least the height of a caterpillar. Say that the caterpillar with the commas for legs is on a nice green field of December grass after a whole month of November rain. The square braces are taller and spread all around in a wonderful game of croquet. Throughout our playing field, we can add to our croquet game but the stations must go in order. Each of those stations carries a true or false. Yes, we have an array of booleans all initialized to false. Right out of the gate at the zeroeth croquet station, our caterpillar gets a true for passing. And so on and so forth until the caterpillar passes the final station and sets them all to true. The caterpillar's day goes well: eats, hoops, and leaves.

Say for sake of using Hashes in our game, we assign extra values to our square brace stations. Instead of an Array of booleans, we now have an Array of Hashes. Our first key would be something like 'pass' and hold our caterpillar's True or False. But we could have so many other keys. Like 'points' with positive or negative Integers. Ah, chutes and ladders. And how about 'name' => 'The Ninth Hole' and 'designer' => 'Desi Arnez' and 'height' => '10em'? We could keep a small dictionary of information about each of our stations, holding sway on our green croquet playing field.