To hash or not to hash? Was that the question?
For the normals among us out there, the intricacies of programming languages like Ruby are about as inaccessible as ancient Egyptian. But upon closer examination they actually have some straight-forward built in objects that simplify things like processing and storing information. Two such objects are arrays and hashes. An array as you might expect is a list of items that are stored in the memory of a program bracketed with brackets [ ]. It will store any object in Ruby such as numbers, , variables, strings, booleans, even arrays and hashes, all separated by commas. An array that holds one of each of these objects might look something like this:
my_array = [42, variable1, “What’s up?”, true, [“Another”, “array!”], { }]
Arrays are organized in order according to their index, which is to say their position in the array starting with the number 0 instead of 1. So 42 has the index of 0, variable1 has an index of 1 and so on. The utility of an array becomes apparent when you start to get into array methods that are built into the language of Ruby. There are methods that operate on arrays to check each index for something, join strings into sentences, pick specific values etc. This is probably about one percent of possible methods for arrays so they are extremely useful. A hash is a similar object, but they have some differences that make them more appropriate than arrays in certain circumstances. The main difference is that within a hash the information isn’t stored as indices; they are stored as pairs of keys and values, so the info isn’t necessarily accessed in order, it’s accessed according to its key. The other main distinction is in their syntax, hashes are denoted with curly braces:
my_hash = {
city: “San Francisco”,
state: “California”,
country: "USA”,
planet: “Earth”
}
Keys and values should be one of the objects previously mentioned. In this syntax the keys will auto- matically become another type of Ruby object, a symbol, which means it will be unique to that specific key and not able to be duplicated anywhere else. This syntax is new to Ruby 1.9 and makes key naming a little bit easier than in the past. Hashes have many of the same methods that arrays do, but are more useful in situations where you need data to be specific to a certain key or value.
For instance if I wanted to know what the highest grade on the math test was, I could put all the grades
in an array and use a method to return the highest number in the array. However if I wanted to attach a
student to that highest grade I would want to put all the students with their grades as key-value pairs in a
hash, and use a method to return the student that earned the highest grade. There are endless possibilities,
but the moral of the story is arrays are easier to work with and process, whereas hashes are more versatile.
Thanks for reading and I hope while doing so you learned something about arrays and hashes , I sure did
while writing it!