Home »
Ruby Tutorial
Ruby Arrays
By IncludeHelp Last updated : November 17, 2024
An array is an ordered list of elements of same data type stored at contiguous memory locations. It is a collection of identical elements referred by a common name.
Ruby Array
In Ruby, an array is a group of objects and we know that even Integer which is generally of primitive type is also an object. So arrays can be used to store an integer, number, string, hash, symbol, object or even any other array. The indexing of an array generally starts with 0 like in other languages.
Creation of Array
An array can be created by grouping elements which should be separated by , and enclosed between square brackets ([]). In Ruby, the following are valid ways to create a 1-D array,
1. Using "new"
Arrays can be created using a new class method by applying the dot operator with the keyword "Array". The array is a predefined class name in Ruby library.
Syntax
array_name= Array.new
Example
#array creation without parameters
array1 = Array.new()
#array creation with one parameter i.e. Size of array
array2 = Array.new(4)
#array creation with two parameters i.e.
#size and element
array3 = Array.new(4, "Includehelp")
puts array1.length
puts array2.length
puts array3.length
puts "#{array3}"
puts "#{array2}"
Output
0
4
4
["Includehelp", "Includehelp", "Includehelp", "Includehelp"]
[nil, nil, nil, nil]
You can observe that if there is no element present in the array, it will display nil.
2. With the help of literal constructor[]
Opening [ and closing square ] brackets creates a literal constructor in Ruby. The only identical type of value can be assigned between [].
Syntax
Array_name = Array[element1, element2, element3, ..., element n]
Example
#creation of array using literal constructor.
array1 = Array[ 'A' , 'B', 'C', 'D' , 'E' , 'F']
#printing array elements, size and length
puts "Length of array is: #{array1.length}"
puts "Size of array is: #{array1.size}"
puts "Array elements are #{array1}"
Output
Length of array is: 6
Size of array is: 6
Array elements are ["A", "B", "C", "D", "E", "F"]
Accessing Elements of an Array
There are several ways to retrieve elements from an array but indexes are commonly used to cope up the purpose.
Example
A single element can be accessed in the following way:
#creating string using literal constructor[]
str = Array["Includehelp", "Ruby", "C#", "C++"]
#retrieving and printing single elements
puts str[2]
puts str[1]
puts str[0]
Output
C#
Ruby
Includehelp
Accessing Multiple Elements at the Same Instant
Most of the times user may need to access multiple elements at the same instant.
Example
It can be done as follows:
#creating string using literal constructor[]
str = Array["Includehelp", "Ruby", "C#", "C++"]
#accessing and printing multiple elements
#at the same time
print str[2,3]
puts str[3]
puts str[0]
Output
["C#", "C++"]C++
Includehelp
Iterating Over an Array
There are several ways through which you can put your array into the loop. One of the ways is by using for loop.
Example
This can be implemented in the following manner:
str = Array["Includehelp", "Ruby", "C#", "C++"]
for i in str do
puts i
end
Output
Includehelp
Ruby
C#
C++
Arrays Methods
Here is the reference of the Ruby array methods: Ruby Array Methods