Let's say I have the following array:
array = ["a","a","a","a","a","a","b","b","b","b","b","b"]
I want to find the index of the first "b" in the array. What is the best way of doing it?
Use Array#index
for first occurrence and Array#rindex for last occurrence of an element.
array = ["a","a","a","a","a","a","b","b","b","b","b","b"]
array.index("b") # => 6
array.rindex("b") # => 11
indexwill find the first "b" without trouble:'aabb'.index('b') => 2.