Ruby Array

Ruby Array

The Ruby array is created using the objects between [ and ]. Ruby arrays are ordered collections of elements.

Array syntax

myArrayName = Array.new  

Array new class method example

myArrayName = Array.new(30)
puts myArrayName.size  
puts myArrayName.length 

Output

30

30

Array Elements example

arrName = ["A", "B", "C", "X", "Y", "Z", "1", "2", "3"]   
puts arrName.at(0)   
puts arrName.at(2) 
puts arrName.at(3) 
puts arrName.at(6) 
puts arrName.at(8) 

Output

A

C

X

1

3