OneBite.Dev - Coding blog in a bite size

add new item in array in Ruby

Code snippet on how to add new item in array in Ruby

my_array = [1,2,3]
my_array << 4
#my_array is now [1,2,3,4]

This code adds a new element to the end of the array my_array. The first line creates an array object with elements 1, 2, and 3. The second line uses the shovel operator (<<) to add the number 4 to the end of the array. The new array object, my_array, now contains the elements 1, 2, 3, and 4.

ruby