Ruby: Intro to Arrays Part 1

Ines Guerrero
6 min readApr 28, 2019

--

I’m one week into an immersive coding bootcamp and I’ve learned so many concepts, it’s hard to keep track of everything. I’ve especially experience one-second victories. That feeling when you finally get it and then, one second later, you realise that you only got one piece of the puzzle — “Welcome to Software Engineering”, said everyone.

Whilst learning Object-Oriented Ruby, I had to manipulate arrays [] and hashes {}. Personally, I found working with arrays a lot simpler than hashes, however, here comes the question that took me hours to understand: how can I change the elements in an array to a hash format? First, let’s go back to basics.

1. Arrays

An array is a collection of one or more items of a data type (strings, integers, other arrays, hashes, symbol, etc.). Each item in the array is an element. Each element is separated by a comma ( , ) and the array is wrapped in square brackets [ ]. Whereas arrays can hold different types of data, it is best practice to only have one type of data within an array.

Arrays work like lists, they help you group elements of the same type. Let’s say you are planning a barbecue and you have a long list of items that you need to buy at the grocery store.

Creating an array

You could start by creating a variable that holds one of the elements on your list: bbq_item = [“tomato”] But what about the other items? You also need beef patties, cheddar cheese, lettuce, buns, crisps, coleslaw, and sausages. Should we add these one at a time?

Creating variables

It’s too time-consuming, isn’t it? To save time, let’s create an array that holds all of these strings. There’re two different ways to create an array: the literal constructor and the class constructor. I’m going to use the literal constructor:

bbq_meal = [“tomato”, “beef patty”, “cheddar cheese”, “lettuce”, “bun”, “crisps”, “coleslaw”, “sausage”]

Creating an array of strings

Finding an element in the array

Elements in an array are identified with an index number. In arrays, indexes start at 0 (zero). Let’s have a look at our bbq_meal array. The first item is “tomato”, therefore, “tomato” is located at index 0. The following item, “beef patty” is located at index 1, and so on. In this case, the last item in the array, the “sausage”, is located at index 7. However, if you have an array with hundreds of item, do you really want to count them all? (You could, but let’s ignore that for now). In arrays, the last element of the array is located at index -1.

To find the element in a corresponding index location, we type the array’s name followed by the index number in brackets, as seen below:

bbq_meal[0] = “tomato”

Creating an array | Elements in an array and their index locations

Adding elements to an array

The most common method to add elements to an array is the “shovel method” (<<). You can read about other methods here. We will stick to this one, for now. The shovel method allows you to add items to the end of an array. Let’s say we forgot to add food for our veggie friends, oops. We want to add mushrooms and halloumi to the array. Should we re-write the array? No. Instead, we will use the shovel method. (There’s a reason why I’m not writing the ingredients in plural… you’ll see why later).

bbq_meal << “mushroom”

bbq_meal << “halloumi”

We could also add these elements in one go, but we will keep it simple, for now. Now we can see the mushroom and the halloumi as elements in our bbq_meal array. Before this bootcamp, I knew nothing about coding and one of the things that I didn’t understand is how to read some things in “English” instead of code. One of the things was the shovel method. First, for some reason, the shovel method is read from right to left. We are saying: “We want to shovel ‘mushroom’ into the ‘bbq_meal’ array”

Adding elements to an array using the shovel << method

The include? method

Imagine our BBQ is for 100 people. This would result in an array with lots of elements. How can we check if everything is there? We have a celiac friend who loves her beer, but beer has gluten! So, we need to check if we’ve included gluten-free beer to our list. Do we go through all the elements in the array? I’m sure you know now that the answer is no.

There is a method called “include?” that will help us, as long as we know the name of our element, of course. We can check by typing the name of the array followed by .include? and then the element that we are looking for:

bbq_meal.include?(“gluten-free beer”)

This will return either true or false. In this case, the method will return false as we have not included gluten-free beer (or any kind of beer!) in our list. Now that we know how to add elements to the array, we can use the shovel method to add gluten-free beer and any other item we’d like.

Adding multiple elements

Now that we’ve seen how to shovel elements into an array, let’s see what options we have to add the same element multiple times. Going back to our BBQ, we can see that we need beef patties (the most important part of the burger!). But, how many do we need? 5? 10? 20? Should we shovel 4 more patties into the array? Let’s try it.

bbq_meal << “beef patty”

bbq_meal << “beef patty”

bbq_meal << “beef patty”

bbq_meal << “beef patty”

That seems like a lot of work and like everyone at the Flatiron School says: “As programmers, we are lazy and that’s a good thing!” What we want is to add multiple items at once so we will shovel an array into an array. First, let’s create a second array called missing_items.

missing_items = [“gluten-free beer”, “beef patty”, “beef patty”, beef patty”, “beef patty”, “beer”, “wine”]

Now, we will shovel the missing_items array into the bbq_meal array.

Adding an array to an array using the shovel method

Hmm. That looks kind of ugly, doesn’t it? It is literally an array within an array and what we want is to add only the elements of that array without any extra brackets []. So, we use the flatten method.

The flatten method

After adding the missing_items array to the bbq_meal array, we were left with extra brackets, making our code look messy and we don’t like that. So, we use the flatten method to eliminate those extra brackets.

bbq_meal.flatten

The flatten method is returning a new array with all the elements. Therefore, our original array remains unchanged.

Flatten method

However, what if we want our bbq_meal array to change? We use the flatten! method.

bbq_meal.flatten!

Flatten! method

Great! Now, to keep it simple, let’s pretend that our array holds everything that we want to buy. In Part 2, we will see how to push items from an array into a hash so we can see the elements in a more organised way and manipulate them more, particularly when we have more than 1 of the same item (i.e. 10 beers, 20 patties, 5 bags of crisps). Furthermore, this will allow us to iterate over nested hashes and extract the value (or in this case the “how many”) of each item i.e. how many patties are buying.

That was my reaction.

But don’t worry. In, Intro to Hashes, we’ll review the basics of hashes and also continue working on our BBQ list. Have a look and you’ll learn how to create a hash based on the info in an array!

Part 2: Intro to Hashes

--

--