How to get index of array element in Ruby

Ruby Array contains method index() to get the selected element index inside array.

Syntax:

array_instance.index()

 

Example:

# array declaration

lang = ["Java","Flutter","ReactNative","Ruby","Perl"]

puts "Array index implementation."

puts "Enter the element whose index you want to find:"

ele = gets.chomp

if(ind = lang.index(ele))

  puts "#{ele} found at index #{ind}"

else

  puts "Array doesn't contains given element"

end

 

Output

Array index implementation.
Enter the element whose index you want to find:
 Flutter
Flutter found at index 1

 


Subscribe For Daily Updates

Flutter Questions
Android Questions