To access array elements in Ruby using index, you can use square brackets notation.
In this example,
numbers
with integer values.2
using indexing numbers[1]
.numbers = [1, 2, 3, 4, 5]
puts numbers[1]
2
In this example,
chars
with character values.3
using indexing chars[2]
.chars = ['a', 'b', 'c', 'd', 'e']
puts chars[2]
c
In this tutorial, we learned How to Access Array Elements using Index in Ruby language with well detailed examples.