OneBite.Dev - Coding blog in a bite size

insert an element at the beginning of an array in Ruby

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

  array = [1, 2, 3]
  array.unshift(0)

This code shows how to insert an element at the beginning of an array in Ruby. First, the array is initialized with three elements, 1, 2, and 3. Then, the unshift method is used on the array, which inserts the element passed to it (in this case, 0) at the beginning of the array. The result of running this code is that the array now contains the elements 0, 1, 2, 3.

ruby