Ruby | Array class find_index() operation
Last Updated :
08 Jan, 2020
Improve
Array#find_index() : find_index() is a Array class method which returns the index of the first array. If a block is given instead of an argument, returns the index of the first object for which the block returns true.
Ruby
Output :
Ruby
Output :
Syntax: Array.find_index() Parameter: block - condition to follow Return: index value of the array elementsCode #1 : Example for find_index() method
# Ruby code for find_index() method
# declaring array
a = [18, 22, 33, nil, 5, 6]
# declaring array
b = [1, 4, 1, 1, 88, 9]
# declaring array
c = [18, 22, nil, nil, 50, 6]
# find_index
puts "find_index : #{a.find_index(5)}\n\n"
# find_index
puts "find_index : #{b.find_index(4)}\n\n"
# find_index
puts "find_index : #{c.find_index(nil)}\n\n"
find_index : 4 find_index : 1 find_index : 2Code #2 : Example for find_index() method
# Ruby code for find_index() method
# declaring array
a = ["abc", "nil", "dog"]
# declaring array
b = ["cow", nil, "dog"]
# declaring array
c = ["cat", nil, nil]
# find_index
puts "find_index : #{a.find_index("abc")}\n\n"
# find_index
puts "find_index : #{b.find_index(nil)}\n\n"
# find_index
puts "find_index : #{c.find_index(nil)}\n\n"
find_index : 0 find_index : 1 find_index : 1