Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
An array is a container of data, similar to a list. In this video, we'll learn what arrays are and how to create them.
Code Samples
Create an array:
array = Array.new
Create an array with three items:
array = Array.new(3, "Jason")
Create an empty array with bracket notation:
array = []
Create an array using brackets with three items:
array = ["milk", "eggs", "bread"]
Create an array using the %w
notation which contains three strings:
array = %w(milk eggs bread)
Create an array using the %W
notation which contains three strings and one is interpolated:
item = “milk”
array = %W(#{item} eggs bread)
[MUSIC]
0:00
Hi, I'm Jason.
0:04
Let's start talking about arrays in Ruby.
0:06
So far, some basic types that we've
learned about are strings and numbers.
0:09
Strings let us work with textual
data in our programs, and
0:14
numbers with numerical data.
0:18
Let's say that we wanted
to create a shopping list.
0:21
Inside of the shopping list,
0:24
our grocery items could be something
like milk, eggs, and bread.
0:25
We could store that list in
a string separated by commas.
0:30
Or we could use three different strings
called something like item one,
0:34
item two, item three, and so on.
0:38
However, Ruby gives us a type specifically
to work with that kind of data.
0:41
We could use an array.
0:46
An array is a container of data.
0:49
You can think of it as a list.
0:52
We could store all of our different
grocery items inside of the array.
0:54
Arrays can store any type of thing
that we have access to in Ruby.
0:59
Let's see how to make an array
now using WorkSpaces.
1:04
So I have gone in and
created a new Ruby workspace.
1:08
Now, over here in the console,
going to drag this up.
1:14
And we're going to launch IRB
by typing irb and press Enter.
1:19
Now let's go ahead and
create our very first array.
1:25
We'll do that by typing
the name of the variable,
1:30
in this case we'll say we
wanna create a grocery list.
1:33
Now I'm going to type a space and
the equals sign, another space, and
1:38
then capital a, y,
a dot, and the word new.
1:43
This will create a new array, initialized
to the variable called grocery list.
1:51
Now, right below,
you'll see we have an equal sign, and
1:57
a greater than sign,
which is going to be the return value.
2:01
And the return value is an open bracket,
and then a closed bracket.
2:05
That's Ruby's sign for an array.
2:09
Now we can take a look at this grocery
list item and it returns the same thing.
2:13
There’s another way that
we can create an array.
2:19
And that is to use
the brackets themselves.
2:24
So, we can type an open square bracket and
then a closed square bracket and
2:26
we get the same thing.
2:30
Now this is useful, but
2:32
it’s not as useful as having a grocery
list with items inside of it.
2:34
Lets go ahead and create a grocery list
array with our items already in it.
2:40
So we'll put three items in here.
2:48
The way we add items to
an array when creating it,
2:51
is to put the items inside of
the brackets, separated by commas.
2:54
So right here, I've opened the bracket and
will put the string milk, and
3:00
then we'll also add eggs and
bread into this array.
3:05
Now we separate the items by a comma.
3:09
So in this case, we have milk as an item,
3:13
and then a comma, eggs,
another comma, and bread.
3:16
We don't put a closing comma
before the closing square bracket.
3:20
Now when we look at the grocery list,
it has the same thing as an empty array,
3:27
except the items are inside.
3:31
Now we're not limited to just strings.
3:35
We could add numbers as well.
3:38
And you'll notice the numbers
don't have quotes around them,
3:41
just like when we were working with
strings and numbers by themselves.
3:44
Now another way to create an array, if we
wanted to create an array of just strings,
3:50
we could use the %w syntax.
3:57
And then we don't have to
surround our strings with quotes.
4:01
Now if we look at our grocery list,
it has the same thing as if we did
4:09
surround it with quotes, and
use the square bracket notation.
4:14
Another thing that we can
do is use an uppercase W.
4:21
Now just like when we were printing
things out with strings and
4:26
having them interpolated, using a capital
W will allow us to do interpolation.
4:29
So I have assigned a variable called
item equal to a string called milk.
4:38
Now, when I create this grocery
list array, using a capital W,
4:43
item will be interpolated.
4:47
And the array comes back with a string
of milk, another string of eggs, and
4:49
another string of bread.
4:54
Try both methods of creating arrays now,
using WorkSpaces.
4:58
You need to sign up for Treehouse in order to download course files.
Sign up