Home » Ruby programming

Creating Two Dimensional Arrays in Ruby

Ruby Two Dimensional Array: In this tutorial, we are going to learn about two-dimensional array in Ruby programming language, creating a two-dimensional array, accessing array elements and accessing subarrays, etc.
Submitted by Hrithik Chandra Prasad, on December 25, 2019

Ruby Two Dimensional Array

So far we have discussed single-dimensional Array instances or 1D Array instances in Ruby. We have seen how we can declare them and how we can implement Array class methods on them. Ruby provides you many methods through which you can manipulate or shorten your code. In this article, we will see how we can declare and implement 2-D Array objects or the two-dimensional Arrays in Ruby? There is nothing like Two Dimensional Array class in Ruby or you can say that there is no separate class for double–dimension Arrays because two-dimensional arrays are nothing but the combination of two 1D Arrays. In this article, you will go through two ways through which you can declare two-dimensional arrays in Ruby.

The first way is a conventional way and the second way is away with some twist and they both are given below,

1) Declaring with the help of [] block

This is the very easiest and conventional method of declaring a 2D array. You only have to assign the values to the Array instance with the help of Assignment operator and square braces. The syntax and demonstrating example is given below,

    array_name = [ [val1,val2], [val3,val4], ..., [valm,valn] ]

Example:

=begin
    Ruby program to create 2 D Array
=end

# a two-dimensional array declaration
arr = [[1,2],[2,3],['Satish','MCA'],['Hrithik','BCA']]

# printing
puts "The two dimensional Array elements are:"
print arr

Output

The two dimensional Array elements are:
[[1, 2], [2, 3], ["Satish", "MCA"], ["Hrithik", "BCA"]]

Explanation:

In the above code, you can observe that we are creating a two-dimensional array most conveniently. We only have to assign values to the array_name and we are done with creating a two-dimensional array as we have done in the program code.

2) With the help of Array.new method

We can create a two-dimensional array with the help of Array.new method as well. Only we have to pass the Array.new method as one of the arguments of the outer Array.new() method.

We will understand the scenario in a better way when you will understand it with the help of Syntax and examples.

Syntax:

    array_name = Array.new(size, Array.new)

Example:

=begin
    Ruby program to create 2 D Array
=end

# array declaration
arr = Array.new(2,Array.new)

# assigning values
arr[0][0] = "Hrithik"
arr[0][1] = "Includehelp"
arr[1][0] = "Hrithik"
arr[1][1] = "Includehelp"

# printing
print "Array elements are...\n"
print arr

Output

Array elements are...
[["Hrithik", "Includehelp"], ["Hrithik", "Includehelp"]]

Explanation:

In the above code, you can observe that you can create the Array in the above way as well. Later on, you can assign values in the above ways as well.



Comments and Discussions!

Load comments ↻





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