DEV Community

brylenelavelle
brylenelavelle

Posted on

Ruby: Creating an Array and Addition Functions

Create an array:
array_name = []
array_name = Array.new

Adding items to the array:
array = ["milk", "eggs", "bread"]
array = %w(milk eggs bread)

Adding "carrots" to the end of the array:
array << "carrots"

Adding the string "potatoes" to the end of the array:
array.push("potatoes")

Adding the string "celery" to the beginning of the array:
array.unshift("celery")

Adding "ice cream" and "pie" to the end of the array"
array += ["ice cream", "pie"]

Adding the number 1 to the end of the array:
array << 1

Top comments (0)