OneBite.Dev - Coding blog in a bite size

insert an element at the end of an array in Ruby

Code snippet on how to insert an element at the end of an array in Ruby

array = [1, 2, 3]
array << 4

This code starts off by setting an array of three elements, 1, 2, and 3. Then, a new element, 4, is added to the end of the array using the << operator. This operator adds an element to the end of the array while modifying the array in-place, meaning that it doesn’t create a new array. This code would result in an array equal to [1, 2, 3, 4].

ruby