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

Arrays

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.

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 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]

Output

C#
Ruby
Includehelp

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]

Output

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

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
end	

Output

Includehelp
Ruby
C#
C++



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.