Home » Ruby programming

Creating Array Instance with + Operator(new_array -> arr + old_array) in Ruby

Creating Array in Ruby: In this tutorial, we are going to learn how to create an array Instance with + operator(new_array -> arr + old_array) in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on December 27, 2019

In the last article, we have gone through the method by which we can create Array instance with the help of * operator. You all must know that in the articles which were related to Array creation are Public class methods and now in the upcoming articles, we will be learning about Public instance methods.

Now, you all must be thinking about what is the difference between Public class methods and Public instance methods, then to clear your doubt Public class methods can be invoked as soon as Ruby processes the execution of the class whereas Public instance methods can only be executed after the creation of an Instance of that particular class. We have studied methods like Array.new() which is a resident of Public class methods but now will be studying Public instance methods. Well, in this article, we will be studying about creating the Array instance with the help of + operator.

Method description:

The + symbol is one of the ways to create an Array object and it requires two previously defined Array instances. It is also known as Set addition or concatenation. This can be an alias for Array.concat(Arr1). It works in the way that it returns a new Array instance having elements after adding the elements of the Array with another Array passed as the argument in the method. The order is maintained from the original Array.

Syntax:

    new_array = old_array + old_array1

Parameter(s):

The '+' operator takes two arguments. First one is the previously defined Array and second one is another Array.

Example 1:

=begin
  Ruby program to create Array with + operator
=end

# arrays
old_arr1 = [23,44,66]
old_arr2 = [33,56,22]

# creating a new array
new_arr = old_arr1 + old_arr2

# printing the array
puts "The new Integer Array Instance is :"
print new_arr

Output

The new Integer Array Instance is :
[23, 44, 66, 33, 56, 22]

Explanation:

In the above code, you can observe that we are creating an Array instance with the help of + operator. In resulting Array, you can see that the new Array is containing all the elements which are present in both the Arrays.

Example 2:

=begin
  Ruby program to create Array with + operator
=end

# arrays
old_arr1 = ['Ramit','Amit','Suresh']
old_arr2 = ['Payal','Samir','Sonakshi','Hira','Panna']

# creating a new array
new_arr = old_arr1 + old_arr2

# printing the array
puts "The new String Array Instance is:"
print new_arr

Output

The new String Array Instance is:
["Ramit", "Amit", "Suresh", "Payal", "Samir", "Sonakshi", "Hira", "Panna"]

Explanation:

In the above code, you can observe that we are creating a String Array with the help of + operator. The resulting Array is having all the Strings which are present in both the Arrays which are passed as the arguments in the method.




Comments and Discussions!

Load comments ↻






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