This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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 upRelated 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