Ruby: Intro to Arrays Part 1

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 stringsFinding an element in the arrayElements 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 locationsAdding elements to an arrayThe 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 << methodThe include? methodImagine 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 elementsNow 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?.105.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 methodHmm.

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 methodAfter 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.


flattenThe flatten method is returning a new array with all the elements.

Therefore, our original array remains unchanged.

Flatten methodHowever, what if we want our bbq_meal array to change?. More details

Leave a Reply