Arrays
Goals
- Understand how arrays are used in real web applications
- Store multiple pieces of data in on variable using an array
- Use developer skills to discover how arrays work
Collections & Lists - in Our Daily Lives
Before we talk about this concept of Arrays in the context or programming or Ruby, let’s connect it to some real-world examples.
We can think of an Array as a collection or list that contains items of the same type. Some examples follow:
- I can write a grocery list of all the things I need to get from the store today.
- I have a jar that holds all my writing utensils - pens, pencils, highlighters.
- I have a cabinet full of coffee mugs in my kitchen
Share Out: Collections & Lists 1
Look around the space you are in - do you see a collection of anything? Do you keep lists physically written down?
Type your example into the chat. Don't send the message yet; we'll all send them at the same time!
Collections & Lists - in Applications
We have this idea of collections and lists down, but you may be wondering “what does this have to do with building an app?” - great question!
Let’s now brainstorm some collections or lists we see when using web or mobile applications. Some examples follow:
- On my weather app, I see a list of the upcoming days and forecast for each of those days.
- On my workout app, I see a list of my completed workouts
Share Out: Collections & Lists 2
Think back to the app you mentioned earlier. Where do you, as a user, see a list?
Type your example into the chat. Don't send the message yet; we'll all send them at the same time!
Arrays
Arrays are the structure that Ruby gives us to hold multiple pieces of data - like a collection or list!
Arrays exist in most other programming languages as well; but the way we write the exact code probably differs a bit!
We can also work through the following sections in this repl.it
Just like Strings, we always want to store our arrays in a variable. The variable name should be plural. In Ruby, we declare an empty array like this:
completed_workouts = []
Alternatively, you could start with items already in your array. Each item in an array is referred to as an element
. Here’s an array that holds Strings:
completed_workouts = ["10 min arms", "30 min ride"]
puts completed_workouts
If we just want to acces one element from an array, we can use bracket notation
and a number that corresponds with that element. As weird as it may seem, counting starts with 0 in most programming languages.
completed_workouts = ["10 min arms", "30 min ride"]
puts completed_workouts[0]
# "10 min arms"
puts completed_workouts[1]
# "30 min ride"
Adding Elements
You can also add items to your array using the shovel operator
, like so:
completed_workouts = ["10 min arms", "30 min ride"]
completed_workouts << "15 min stretch"
completed_workouts << "20 min HIIT"
print completed_workouts
#the completed_workouts array now holds 4 elements
Try It: Arrays in your App
Earlier, you brainstormed your favorite or most-used app, and a place where an array would be used in it. Using that idea, let's get to work!
- In a repl.it, create a new variable that holds an array. This array should hold data (yes, faked data) related to your brainstorm from above. Your array can hold as many elements as you'd like it to!
- Practice using
bracket notation
to print out an individual element from your array. - Lastly, use the
shovel operator
to add at least one element to your array. Make sure to print or puts our your array after doing so, to verify that the new element is being stored.
Removing Elements
There are many ways we can remove elements from an array. The code below demonstrates the pop
and shift
array methods. They may remind you of the String methods we learned earlier (capitalize
, upcase
, etc.) - and that is spot-on!
Read the code below to process and familiarize yourself with it; we haven’t yet discovered how pop
and shift
work:
completed_workouts = ["10 min arms", "30 min ride", "15 min stretch"]
completed_workouts.pop
print completed_workouts
followers = ["@letacodes", "@j3", "@jwanliu"]
followers.shift
print followers
Try It: Removing Elements
In the same repl.it you've been working on your arrays, try using the pop
and/or shift
methods. Use puts
or print
statements to compare the before and after, and see if you can deduce how they work!
Please be ready to share out!