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)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay