OneBite.Dev - Coding blog in a bite size

get the first element of array in Ruby

Code snippet on how to get the first element of array in Ruby

  array = [1,2,3,4,5]
  first_element = array[0]

This code shows how to get the first element of an array in Ruby. The first line of code creates an array (called ‘array’) with five elements: 1, 2, 3, 4, and 5. The second line assigns the variable ‘first_element’ to the first element in the array. This is done by accessing the array at the index 0, which returns the first element. In this case, the variable ‘first_element’ is assigned the value 1.

ruby