To find the index of an element in an array in Ruby, you can use the Array#index
method. Here's an example:
main.rb96 chars7 lines
In the example above, we first create an array of integers, and then we define the element
variable to store the value we're looking to find the index of. We then call the index
method on the array object and pass our element
variable as an argument. The index
method then returns the index of the first occurrence of the element in the array.
If the element is not found in the array, the index
method will return nil
.
gistlibby LogSnag