Adding Items to Arrays3:26 with Jason Seifer
Now that we know how to create a Ruby array, let's learn a few ways to add 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. 0:00 Once we have an array, 0:05 there are a few different ways of adding items to that array. 0:07 Let's see how that works now using workspaces. 0:10 So here's another workspace, and I've created a file called arrayaddition.rb. 0:14 If you want to check out what we did in IRB in the last video, 0:20 it'll be there in array_creation.rb. 0:23 So now let's go ahead and check out this file. 0:27 Here we have our grocery list array. 0:31 It contains three items, milk, eggs and bread. 0:34 We can see the contents of an array if we run it using a Ruby program file by typing 0:40 puts, and we can do grocery_list.inspect. 0:44 And that will print out the contents of the array onto the screen. 0:50 Let's go ahead and run this file by typing ruby array_addition.rb. 0:56 Now we can see we have our array. 1:02 Let's go ahead and add something on to this. 1:06 So I've just clicked up onto line two and if I want to add on to the grocery list, 1:09 I could say grocery_list and then two less than signs and add another item. 1:16 And that will append the item onto the end of the array. 1:24 Now if I run this, 1:30 we can see that there is another item at the end of the array called carrots. 1:31 Another method of adding something to the end of an array 1:37 is to use a method called push. 1:42 So we could say grocery_list and then a dot and then push and 1:45 then open parentheses and the word Potatoes. 1:50 This is getting to be a pretty big grocery list. 1:54 Now if we print this out, we can see we have carrots and 1:58 potatoes at the end of our original array. 2:01 But what happens if we wanted to add something to the beginning of the array? 2:06 In that case, we could use the method called unshift. 2:10 Which, instead of adding something to the end of the array, 2:17 we'll add it to the beginning. 2:21 And we'll put celery at the beginning of the grocery list 2:24 because we're eating healthy. 2:27 Now if we run this, we can see that celery is here at the beginning 2:30 as opposed to carrots and potatoes. 2:35 Which were at the end. 2:37 If we wanted to we could also use 2:40 the plus equals operator and add more items onto the array that way. 2:44 Let's go ahead and get some desserts in here. 2:50 So we'll add ice cream and pie. 2:53 And you'll notice that I'm adding another array here. 2:57 Let's run that and see what happens. 3:01 and it looks like ice cream and 3:04 pie were successfully added to the end of our array. 3:05 Now, we're not limited to just strings. 3:10 We could also add numbers if we wanted as well. 3:14 Or actually, almost any Ruby type can go in there. 3:17 Try adding on to your arrays now using WorkSpaces. 3:22
You need to sign up for Treehouse in order to download course files.Sign up