Picture of myself

How Arrays Work

January the 7th in year 2015

The fun times are over, Christmas presents are exchanged, and shopping centres made a lot of money. And what does this have to do with what I am about to write? Nothing, really. But you have to start your blog somehow, otherwise you just stare in a blank piece of paper - or screen in this case.

So what am I going to talk about today? It's the first thing that I found challenging with programming. Arrays and Hashes. These two aren't that much different, so I'll just explain arrays first and then tell you what's the difference with hashes.

I've looked at Java and C# before I decided to go for Ruby and it was where I got introduced to arrays, and to be honest, it was a very confusing thing for me. I'll tell you how I imagine them now. Let's picture an array as a line of plastic cups. Each of this cups has some space inside. What we try to fill this cups with is so small, that they never get full, they just get heavy. The only rule is that they must stand in a line, and what we need to give them is a platform. They need a table to stand on.

An array looks exactly like this, it's just that you can't see it.

So let's put this terms we established here into code. Cups can't hover in mid-air so they need a table, which we create with command:
Array.new
The catch is, that this isn't enough. Program wants us to name this array of ours, so we need to create a variable that we can name whatever we want, like line_of_cups for example. So the code that computer would except would be:

 line_of_cups = Array.new

One thing we need to know here is, that arrays in Ruby are very closely related to square brackets - []. What we have written here is actually an empty array. In fact writing:

 line_of_cups = Array.new

is the same as

 line_of_cups = []

We could write either of them and we'd get our platform.

Until now we only put down a table for our cups. It's time to add something to our array. The good thing is, that we don't need to put cups on a table, the computer does this for us. We only need to tell him what we want to put in them, and we want to put in them a number 123 (an integer in programming terminology), and "some words" (a string in programming terminology). Strings always need to be in "quotes" so the computer knows how long it is.

Adding to array is pretty straight forward. We tell to the computer which array and what to add. In our case this would be

 line_of_cups << 123

and

 line_of_cups << "some words"

This gives us an array with two objects

=> [123, "some words"]

There is another way to push objects in our array. With this one we'll add an integer 456, a string "more words" and another array that already has a string in it. We'll do that with a command:

 line_of_cups.push(456,"more words",["the new array"])

If we were to test our array, it would now print

=> [123, "some words", 456, "more words", ["the new array"]]

Here we could add many more objects to it with this commands, and our array would never run out of table to put cups on, or cups themselves for that matter.

Let's see how the whole file now looks like, and what does it return:

  line_of_cups = Array.new
  line_of_cups << 123
  line_of_cups << "some words"
  line_of_cups.push(456, "more words",["the new array"])
  p line_of_cups

Output:

=> [123, "some words", 456, "more words", ["the new array"]]

Now we know how to create an array and how to add things to it, so it's time to learn how to count the cups in our array. It's similar, but a little bit different. If we count the fingers on our hand, it would be 1, 2, 3, 4 and 5, right? But the cups in the array start with 0 instead of 1, so they would count our fingers accordingly: 0, 1, 2, 3 and 4. Try to imagine that each of this cups has a number on it, and it so happens that the first one is 0. This numbers are called index and they are important when we want to point to any of our cups. I usually figure out the index of an object by counting normally and then subtract 1.

Why is this important? Well, we need them indexes when we want to look into our cups. The computer needs to know which cup is holding an object we need, and if we know the index, it is really easy. We tell the computer 'go to line_of_cups array and look into a cup with index 2' with a command:

 line_of_cups[2]

Computer would return

=> 456

Just like that! Simple.

Arrays are very useful when we need to store some objects. We have seen before that arrays can hold other arrays as well. This are then called 2 dimensional arrays. But what if we don't want to access the object in arrays with index numbers. What if we want to access them with some words we can make up ourselves? This is then the part when we need to return to the beginning of the blog post and remember that I was saying something about hashes, and they are exactly what we need in this case.

They are very similar to arrays but they have keys to access the value. There, I just said it. Hashes are made out of key / value pairs. Just like arrays are best friends with square brackets - [], hashes are best friends with curly brackets - {}. Just like arrays we need to give our hash a name when we create one, for example:

 whats_my_name = Hash.new

Or like this:

 whats_my_name = {}

To add anything to it, we need to specify what key is the value going to be connected with. So, if we want to add "Marko" string as a value, we need to add "name" as the key.
Like this:

 whats_my_name["name"] = "Marko"

Our hash would now return:

=> {"name" => "Marko"}

There is plenty of good methods we can apply to each of them. Find some methods to call on arrays , and some to call on hashes .

...by Marko Anton Potocnik