Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Now that we know how to create a Ruby array, let's learn a few ways to add items to it.
Code Samples
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:
array.push("potatoes")
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
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
bryanchapman
4,486 Points1 Answer
-
Katarina Lingat
6,421 Points1 Answer
-
Steven Hohberg
6,319 Points1 Answer
-
Sven Vanlandschoot
11,252 Points3 Answers
-
Alex Cevallos
16,551 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up