To find the first element of an array in Ruby, you can use the first
method. Here's an example:
main.rb79 chars4 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
.
gistlibby LogSnag