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
You need to sign up for Treehouse in order to download course files.Sign up