Ruby | Arrays

19 May, 2021
An array is a collection of different or similar items, stored at contiguous memory locations. The idea is to store multiple items of the same type together which can be referred to by a common name. In Ruby, numbers, strings, etc all are primitive types but arrays are of objects type i.e arrays are the collection of ordered, integer-indexed objects which can be store number, integer, string, hash, symbol, objects or even any other array. In general, an array is created by listing the elements which will be separated by commas and enclosed between the square brackets[]. Example:  
["Geeks", 55, 61, "GFG"]
 
Here array contains the 4 elements of different type i.e Geeks(a string), 55(number), 61(number), and GFG(string). Array positive index starts from 0. The negative index always starts with -1 which represents the elements from the end of the array. There can be 2-Dimensional array i.e array will store another array. It is also termed as the nested arrays. Here we will only discuss about 1-Dimensional arrays. 
Creation of 1-D array in Ruby
There are several ways to create an array. But there are two ways which mostly used are as follows:  
Using the new class method: new is a method which can be used to create the arrays with the help of dot operator. Here ::new method with zero, one or more than one arguments is called internally. Passing arguments to method means to provide the size to array and elements to array.Syntax: 
Using the new class method: new is a method which can be used to create the arrays with the help of dot operator. Here ::new method with zero, one or more than one arguments is called internally. Passing arguments to method means to provide the size to array and elements to array.Syntax: 
name_of_array= Array.new
Example: 
Example: 
arr = Array.new
Here arr is the name of the array. Here Array is the class name which is predefined in the Ruby library and new is the predefined method. Note: To find the size or length of an array just use either size method or length method . Both methods will return same value for a particular array. 
Here arr is the name of the array. Here Array is the class name which is predefined in the Ruby library and new is the predefined method. Note: To find the size or length of an array just use either size method or length method . Both methods will return same value for a particular array. 
arr = Array.new(40)
arr.size
arr.length
Now array can hold 40 elements. Here 40 is the size & the length of the array.Program: 
Now array can hold 40 elements. Here 40 is the size & the length of the array.Program: 
Ruby
# Ruby program to demonstrate the# creation of array using new method# and to find the size and length of array # creating array using new method# without passing any parameterarr = Array.new() # creating array using new method# passing one parameter i.e. the# size of arrayarr2 = Array.new(7) # creating array using new method# passing two parameters i.e. the# size of array & element of arrayarr3 = Array.new(4, "GFG") # displaying the size of arrays# using size and length methodputs arr.sizeputs arr2.lengthputs arr3.size # displaying array elementsputs "#{arr3}"
Output:  
Output:  
0
7
4
["GFG", "GFG", "GFG", "GFG"]
 Using literal constructor[] In Ruby, [] is known as the literal constructor which can be used to create the arrays. Between [], different or similar type values can be assigned to an array Example:  
 
Using literal constructor[] In Ruby, [] is known as the literal constructor which can be used to create the arrays. Between [], different or similar type values can be assigned to an array Example:  
Ruby
# Ruby program to demonstrate the# creation of array using literal# constructor[] and to find the size# and length of array # creating array of charactersarr = Array['a', 'b', 'c', 'd','e', 'f'] # displaying array elementsputs "#{arr}" # displaying array sizeputs "Size of arr is: #{arr.size}" # displaying array lengthputs "Length of arr is: #{arr.length}"
Output:  
Output:  
["a", "b", "c", "d", "e", "f"]
[1, 2, 3, 4, 5, 6, 7]
Size of arr is: 6
Length of arr is: 6
 
 
 
Retrieving Or Accessing Elements from Array
In Ruby, there are several ways to retrieve the elements from the array. Ruby arrays provide a lot of different methods to access the array element. But the most used way is to use the index of an array.Example:  
Ruby
# Ruby program to demonstrate the# accessing the elements of the array # creating string using []str = ["GFG", "G4G", "Sudo", "Geeks"] # accessing array elements# using indexputs str[1] # using the negative indexputs str[-1]
Output:  
G4G
Geeks
Retrieving Multiple Elements from Array: There can be many situations where the user need to access the multiple elements from the array. So to access the multiple elements, pass the two specified array index into the [].Example: 
Ruby
# Ruby program to demonstrate the# accessing the multiple elements# from  array # creating string using []str = ["GFG", "G4G", "Sudo", "Geeks"] # accessing multiple array elementsputs str[2,3]
Output:  
Sudo
Geeks
Note: If the user will try to access the element that doesn’t exist in the array then nil will return. Example:  
Ruby
# Ruby program to demonstrate the accessing# of array element that doesn't exist  # creating array using []arr = [1, 2, 3, 4] # accessing the index which# doesn't existputs arr[4]
There will be nothing in the output. 
Akanksha_Rai
akshaysingh98088
Ruby-Basics
Ruby
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.