Don Cheadle is the best version of Captain Planet there is. I hope you’ve already seen the video. If not, go now. I’ll wait.
As it typically happens, I was creating some nested data structure and was reminded of all the different combinations that there are. For example:
An array of arrays.
[ [1,2,3], [4,5,6] ]
A hash of arrays.
{ :lucky => [77,42], :unlucky => [666,13] }
An array of hashes.
[ {:cat=>"meow"}, {:dog=>"ruff"} ]
A hash of hashes.
{ :best_in_life => {:enemies => "crushed"}, :worst => { :meatloaf => "old"} }
And I realized that I hadn’t really played with a hash of hashes much. And now I realize why. It’s really pretty useless. It’s hard to work with and the additional key really isn’t all that useful. I found it much better to just denormalize the key into the data attributes. Anyway, you can see what I mean by reading and running what’s below.
We’re going to create Captain Planet and the planeteers in a 2D hash of hashes and do some searching, iterating and other simple things. This should illustrate also how an array of hashes is a bit better. You’ll see halfway through the program we redefine the planeteers.
Here’s what it spits out:
--------------------------------------------------
Here are our planeteers and their elements:
--------------------------------------------------
{:kwame=>"earth"}
{:wheeler=>"fire"}
{:linka=>"wind"}
{:gi=>"water"}
{:ma_ti=>"heart"}
Find the fire planeteer:
wheeler
--------------------------------------------------
Let's do this a bit cleaner with a better data structure.
--------------------------------------------------
Find the heart planeteer:
ma_ti
--------------------------------------------------
Let's summon Captain Planet!
--------------------------------------------------
Kwame: Earth!
Wheeler: Fire!
Linka: Wind!
Gi: Water!
Ma_ti: Heart!
All: Go planet!