find the first element in an array in ruby

To find the first element of an array in Ruby, you can use the first method. Here's an example:

main.rb
arr = [1, 2, 3, 4, 5]
first_element = arr.first
puts first_element # Output: 1
79 chars
4 lines

In this example, we create an array arr with five elements. Then, we use the first method to retrieve the first element of the array and assign it to the variable first_element. Finally, we output the value of first_element using the puts method. The output of this code is 1.

related categories

gistlibby LogSnag