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 articles, we have gone through many methods through which we can create Array Instances but you all must know that those all were Public class methods and now in the upcoming articles, we will be learning about Public instance methods.

Now, you all must be thinking 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 the way to create an Array object and it requires two previously defined Array instances. It is also known as the Set intersection. It works in the way that it returns a new Array instance having elements that are common to both the Arrays, excluding the duplicate elements present in them. The order is maintained from the original Array.

Syntax:

    aew_array = old_array1 & old_array2

Parameter(s):

The & operator takes two arguments and they both are Array instances.

Example 1:

=begin
  Ruby program to create Array with &
=end

# arrays
old_arr1 = [23,44,66,33,12,77,33]
old_arr2 = [12,23,66,2,44,77,8]

# creating a new array using & operator 
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, 12, 77]

Explanation:

In the above code, you can observe that we are creating an Array instance with the help of & operator. In the resulting Array, you can see that all the elements are residing which common in both the Array are being passed as the arguments.

Example 2:

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

# arrays 
old_arr1 = ['Ramit','Amit','Suresh','Payal','Samir']
old_arr2 = ['Babita','Amit','Suresh']

# creating a new array using & operator 
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:
["Amit", "Suresh"]

Explanation:

In the above code, you can observe that we are creating a String Array with the help of & operator. This operator internally uses .eql? method for faster carrying out a faster checking of Array elements of both the Arrays.



Comments and Discussions!

Load comments ↻





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