Tim Cannady

# [ ]

Collections and Container Objects: Arrays and Hashes

"In programming, you generally deal not only with individual objects but with collections of objects." -Well Grounded Rubyist.

This is where arrays and hashes come into play. "What about strings", you ask? Keep in mind strings are a single object. Each character in a string isn't its own object, as a test to .object_id will show. So it's best to look at strings as single objects, and instead consider our arrays and hashes as the way to contain multiple objects.

Let's take things a little further by looking at the latter. They're similar in that they both contain data. However arrays do this in an 'organized' fashion, where hashes are unordered. Why do we need this? Because sometimes we like things to be orderly, and sometimes we benefit from the casualness of an unordered collection. For example, every Sunday (before Game of Thrones) I put my daily vitamins in respective slots of a pill box. This is an ordered collection, much like an array. Even moreso because it's laid out in a linear fashion Monday-Friday (more about his later). However when when 11:30am rolls around and I have my mid-morning snack of almonds and cashews, I like them to be mixed. I want them to be in a collection like a bowl (or a hash. The digital kind, not breakfast). But I prefer an unorganized mix.

  • Array: arrays are organized groups of data laid out in a linear fashion. The first slot is for the first object, and the next slot is for the next. Want to know which object is in the first slot? Easy, just ask and it will tell you! Just about anything can be stored in a slot - even other arrays! Having trouble imagining this? Just imagine a train pulling a bunch of carts. That's an array. Drop a few carts into one of the carts and you've got an array within an array (also known as a '3d' array).
  • Hashes: hashes are frequently called 'key-value pairs of data.' A key is a like the word "name" and the value is like the name "Tim." Or "dog: Fido." Like arrays, hashes help us by holding a bunch of objects. However they do it in an unorganized manner, like our mixed nut example. Sometimes we just want to store a bunch of data an an unorganized box. For example, if you made an object for each student in the class, there's no real immediate to organize them. At least from the start. Perhaps an alphabetical sort would be beneficial later on, but there's methods for that. Since students needn't be organized inherently, the way you access them is simply by asking for the specific object. Want to query Tim? Just ask the hash about him!

The main takeaway is that arrays and hashes are containers for collections of objects. However they store data differently (organized/unorganized), and consequentially, their data is accessed differently. For arrays you simply use index notation [0-n], and in hashes you can simply ask the object (that is, assuming you know it exists. If you don't, you can ask the hash describe its contents). Furthermore, both have unique looping and iteration techniques when it comes to accessing data from multiple indexes (arrays) or key-value pairs (hashes).