To access array elements in Ruby, you can use indexing with square brackets.
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,
fruits
with string values.fruits = ['Apple', 'Banana', 'Orange']
fruits.each do |fruit|
puts fruit
end
Apple Banana Orange
In this tutorial, we learned How to Access Array Elements in Ruby language with well detailed examples.