To initialize an array in Ruby, you can use array literals or the Array.new constructor.
In this example,
numbers
with integer values using array literals.numbers = [1, 2, 3, 4, 5]
puts numbers.join(' ')
1 2 3 4 5
In this example,
fruits
with character values using the Array.new
constructor.fruits = Array.new(3, 'Apple')
puts fruits.join(' ')
Apple Apple Apple
In this tutorial, we learned How to Initialize an Array in Ruby language with well detailed examples.