在Ruby中使用&运算符(new_array-> arr&old_Array)创建数组实例

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.

在上一篇文章中,我们介绍了许多可以创建数组实例的方法,但是您必须都知道它们都是Public类方法,现在在接下来的文章中,我们将学习Public实例方法。

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.

现在,您必须都在考虑Public类方法和Public实例方法之间的区别,然后为澄清您的疑问,只要Ruby处理类的执行,就可以调用Public类方法,而Public实例方法只能执行在创建该特定类的实例之后。 我们研究了Array.new()之类的方法,该方法是Public类方法的居民,但现在将研究Public实例方法。 好吧,在本文中,我们将研究如何在&运算符的帮助下创建Array实例

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.

&符号是创建Array对象的方法,它需要两个先前定义的Array实例。 也称为Set相交。 它的工作方式是返回一个新的Array实例,该实例具有两个数组共同的元素,但不包括其中存在的重复元素。 该顺序是从原始Array维护的。

Syntax:

句法:

    aew_array = old_array1 & old_array2

Parameter(s):

参数:

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

&运算符带有两个参数,它们都是Array实例。

Example 1:

范例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.

在上面的代码中,您可以看到我们正在使用&运算符创建一个Array实例 。 在生成的Array中,您可以看到所有元素都驻留在两个Array中哪个公共位置作为参数传递。

Example 2:

范例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.

在上面的代码中,您可以观察到我们在&运算符的帮助下创建了一个字符串数组 。 该运算符内部使用.eql吗? 快速执行两个数组的数组元素的快速检查的方法

翻译自: https://www.includehelp.com/ruby/creating-array-instance-with-and-operator-new_array-arr-old_array.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值