I have a secret... I love Ruby (don't tell her). This week has been dedicated to diving into Ruby for the first time. It has been a great experience. There are so many concepts here that it would take forever to cover them all. I'll just stick to two for now. Arrays and Hashes. Hashes and Arrays. Are you ready for a knowledge bomb to be dropped?
Let's take a look at arrays to start. An array is a list of objects. An example looks like [1, 2, 3]
. They do not need to be the same type of objects. You can mix and match different things in an array (i.e. [1, 'String', ['another', 'array']]
). These can be super useful for keeping track of lists of information. Something cool about arrays is that you can access each item in the list by what is called the index. Let's look at an example array, array = ['thing', 'another thing', 'something else']
. Each object in the array has an index. We start at 0 and count up (cause that's what programmers do). So if I were to type puts array[0]
, then we would get 'thing'
. puts array[1]
would be 'another thing'
. Make sense?
Hashes are very similar to arrays. They are lists that have indices that can point to various objects. The major difference is that arrays are great for keeping track of objects in a numbered list whereas hashes don't necessarily need to be in order and there index doesn't have to be a number. I know this sounds confusing so let's take a look. You would set up a hash like this:
hash ={
key => value,
key => value
}
Let's create a sample one.
hash_sample ={
'name' => 'Zac',
'age' => 24
}
Now with an array, I would need to know the order in which each value appeared to be able to call it. But with hashes, I can type puts hash_sample['name']
and it will return 'Zac'
. Pretty cool right? This is very useful when there is a lot of things you want to keep track of but they don't necessarily fit into a nice, neat ordered list.