Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Adding Items to Arrays3:26 with Jason Seifer
Now that we know how to create an array, let's practice adding items to it.
Create an array:
array = ["milk", "eggs", "bread"]
Add the string "carrots" to the end of the array:
array << "carrots"
Add the string "potatoes" to the end of the array:
Add the string "celery" to the beginning of the array:
array.unshift("celery") # add to beginning
Add the strings "ice cream" and "pie" to the end of the array:
array += ["ice cream", "pie"]
Add the number 1 to the end of the array:
array << 1
Now that we know how to create an array, let's look at how to add things to it.
Once we have an array,
there are a few different ways of adding items to that array.
Let's see how that works now using workspaces.
So here's another workspace, and I've created a file called arrayaddition.rb.
If you want to check out what we did in IRB in the last video,
it'll be there in array_creation.rb.
So now let's go ahead and check out this file.
Here we have our grocery list array.
It contains three items, milk, eggs and bread.
We can see the contents of an array if we run it using a Ruby program file by typing
puts, and we can do grocery_list.inspect.
And that will print out the contents of the array onto the screen.
Let's go ahead and run this file by typing ruby array_addition.rb.
Now we can see we have our array.
Let's go ahead and add something on to this.
So I've just clicked up onto line two and if I want to add on to the grocery list,
I could say grocery_list and then two less than signs and add another item.
And that will append the item onto the end of the array.
Now if I run this,
we can see that there is another item at the end of the array called carrots.
Another method of adding something to the end of an array
is to use a method called push.
So we could say grocery_list and then a dot and then push and
then open parentheses and the word Potatoes.
This is getting to be a pretty big grocery list.
Now if we print this out, we can see we have carrots and
potatoes at the end of our original array.
But what happens if we wanted to add something to the beginning of the array?
In that case, we could use the method called unshift.
Which, instead of adding something to the end of the array,
we'll add it to the beginning.
And we'll put celery at the beginning of the grocery list
because we're eating healthy.
Now if we run this, we can see that celery is here at the beginning
as opposed to carrots and potatoes.
Which were at the end.
If we wanted to we could also use
the plus equals operator and add more items onto the array that way.
Let's go ahead and get some desserts in here.
So we'll add ice cream and pie.
And you'll notice that I'm adding another array here.
Let's run that and see what happens.
and it looks like ice cream and
pie were successfully added to the end of our array.
Now, we're not limited to just strings.
We could also add numbers if we wanted as well.
Or actually, almost any Ruby type can go in there.
Try adding on to your arrays now using WorkSpaces.
You need to sign up for Treehouse in order to download course files.Sign up