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 most of the previous articles, we have learned about Public instance methods. For a quick overview, we have studied & operator, * operator, etc. We also know that what is the difference between Public Instance methods and Public class methods. 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 the - operator.

Method description:

This is one of the ways through which you can generate new Array. It works in the way that it returns the copy of the original Array that is the copy of the first Array and this copy does not contain the elements which are present in the second Array. The order which is followed in the new Array is dependent upon the Original Array. This method uses .eql method for efficient processing.

Parameter(s):

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

Example 1:

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

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

# 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: 
[44, 66, 889]

Explanation:

In the above code, you can observe that we are creating an Array instance with the help of the - operator. In the resulting Array, you can see that the new Array is not containing elements that are present in the second Array.

Example 2:

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

# arrays
old_arr1 = ['Ramit','Amit','Suresh','Payal']
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"]

Explanation:

In the above code, you can observe that we are creating a String Array with the help of the - operator. The resulting Array is having all the Strings which are present in the first Array but not in the second Array.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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.