在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.
在上一篇文章中,我們介紹了許多可以創(chuàng)建數(shù)組實例的方法,但是您必須都知道它們都是Public類方法,現(xiàn)在在接下來的文章中,我們將學(xué)習(xí)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.
現(xiàn)在,您必須都在考慮Public類方法和Public實例方法之間的區(qū)別,然后為澄清您的疑問,只要Ruby處理類的執(zhí)行,就可以調(diào)用Public類方法,而Public實例方法只能執(zhí)行在創(chuàng)建該特定類的實例之后。 我們研究了Array.new()之類的方法,該方法是Public類方法的居民,但現(xiàn)在將研究Public實例方法。 好吧,在本文中,我們將研究如何在&運算符的幫助下創(chuàng)建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.
&符號是創(chuàng)建Array對象的方法,它需要兩個先前定義的Array實例。 也稱為Set相交。 它的工作方式是返回一個新的Array實例,該實例具有兩個數(shù)組共同的元素,但不包括其中存在的重復(fù)元素。 該順序是從原始Array維護的。
Syntax:
句法:
aew_array = old_array1 & old_array2Parameter(s):
參數(shù):
The & operator takes two arguments and they both are Array instances.
&運算符帶有兩個參數(shù),它們都是Array實例。
Example 1:
范例1:
=beginRuby 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_ArrOutput
輸出量
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.
在上面的代碼中,您可以看到我們正在使用&運算符創(chuàng)建一個Array實例 。 在生成的Array中,您可以看到所有元素都駐留在兩個Array中哪個公共位置作為參數(shù)傳遞。
Example 2:
范例2:
=beginRuby 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_ArrOutput
輸出量
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.
在上面的代碼中,您可以觀察到我們在&運算符的幫助下創(chuàng)建了一個字符串?dāng)?shù)組 。 該運算符內(nèi)部使用.eql嗎? 快速執(zhí)行兩個數(shù)組的數(shù)組元素的快速檢查的方法 。
翻譯自: https://www.includehelp.com/ruby/creating-array-instance-with-and-operator-new_array-arr-old_array.aspx
總結(jié)
以上是生活随笔為你收集整理的在Ruby中使用&运算符(new_array- arr&old_Array)创建数组实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: scala 字符串占位符_如何在Scal
- 下一篇: 再见Postman,这款API神器更好用