Home » Ruby programming

Ruby Arrays

Arrays in Ruby: Here, we are going to learn about the arrays in Ruby programming language with array creation, accessing array elements, and examples.
Submitted by Hrithik Chandra Prasad, on August 05, 2019


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. 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 in Ruby

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.


    array_name= Array.new


#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}"


["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 [].


    Array_name = Array[element1, element2, element3, ..., element n]


#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}"


Length of array is: 6
Size of array is: 6
Array elements are ["A", "B", "C", "D", "E", "F"]

Accessing elements of Array in Ruby

There are several ways to retrieve elements from an array but indexes are commonly used to cope up the purpose.

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]



Most of the times user may need to access multiple elements at the same instant. 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]


["C#", "C++"]C++

Iterating over an array in Ruby

There are several ways through which you can put your array into the loop. One of the ways is by using for loop. This can be implemented in the following manner:

str = Array["Includehelp", "Ruby", "C#", "C++"] 

for i in str do
	puts i



Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.