obj[]與obj.
Array.rassoc(obj)方法 (Array.rassoc(obj) Method)
In this article, we will study about Array.rassoc(obj) method. You all must be thinking the method must be doing something which is related to the insertion of a certain element. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.
在本文中,我們將研究Array.rassoc(obj)方法 。 大家都必須認為該方法必須執行與插入某個元素有關的操作。 它并不像看起來那么簡單。 好吧,我們將在其余內容中解決這個問題。 我們將嘗試借助語法并演示程序代碼來理解它。
Method description:
方法說明:
This method is Public instance method and belongs to the Array class which lives inside the library of Ruby language. This method is used to check whether an object is a part of the particular Array instance or not and that Array instance cannot be a normal Array instance. If it is not normal, it means that Array instance is the Array of multiple Array instances or you can say that it the collection of multiple objects which are itself an object of Array class. Basically, it works for the Array instances whose elements are also Array instances. Let us go through the syntax and demonstrating the program codes of this method.
該方法是Public實例方法,屬于Array類,它位于Ruby語言庫中。 此方法用于檢查對象是否為特定Array實例的一部分,并且該Array實例不能為普通Array實例。 如果不正常,則表示Array實例是多個Array實例的Array,或者可以說它是多個對象的集合,而這些對象本身就是Array類的對象。 基本上,它適用于其元素也是Array實例的Array實例。 讓我們來看一下語法,并演示該方法的程序代碼。
If you are thinking what it will return then let me tell you, it will return the first contained Array instance where it found the presence of the object. It will return "nil" if it hadn't found the object in any of the Arrays.
如果您在考慮它將返回什么,那么讓我告訴您,它將返回找到對象存在的第一個包含的Array實例。 如果未在任何數組中找到對象,它將返回“ nil”。
Syntax:
句法:
array_instance.assoc(obj)
Argument(s) required:
所需參數:
This method only takes one parameter and that argument is nothing but an object whose presence we want to check.
此方法僅使用一個參數,而該參數不過是一個要檢查其存在性的對象。
Example 1:
范例1:
=begin
Ruby program to demonstrate rassoc method
=end
# array declarations
array1 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]
array2 = ["Akul","Madhu","Ashok","Mukesh",788]
array3 = ["Orange","Banana","Papaya","Apricot","Grapes"]
arraymain = [array1,array2,array3]
puts "Enter the element you want to search"
ele = gets.chomp
if arraymain.rassoc(ele) != nil
puts "Element found in:"
print arraymain.rassoc(ele)
else
puts "Element not found"
end
Output
輸出量
RUN 1:
Enter the element you want to search
Ramesh
Element found in:
[1, "Ramesh", "Apple", 12, true, nil, "Satyam", "Harish"]
RUN 2:
Enter the element you want to search
Kiwi
Element not found
Explanation:
說明:
In the above code, you can find that the Array instance on which we have invoked rassoc() method is not any normal Array instance. It is the collection of multiple Array instances. It is returning the whole Array instance where it has found the object inputted by the user.
在上面的代碼中,您可以發現我們調用rassoc()方法的 Array實例不是任何普通的Array實例。 它是多個Array實例的集合。 它返回找到用戶輸入對象的整個Array實例。
Example 2:
范例2:
=begin
Ruby program to demonstrate rassoc method
=end
# array declaration
array1 = ["Babita","Sabita","Ashok"]
puts array1.rassoc("Babita")
Output
輸出量
No Output.
Explanation:
說明:
In the above, you can verify that rassoc() method does not work upon normal Array instances. It will return nil even if the object is a part of the Array instance.
在上面,您可以驗證rassoc()方法不適用于普通Array實例。 即使對象是Array實例的一部分,它也將返回nil。
翻譯自: https://www.includehelp.com/ruby/array-rassoc-obj-method-with-example.aspx
obj[]與obj.