obj[]與obj.
Ruby Array.include?(obj)方法 (Ruby Array.include?(obj) Method)
In the previous articles, we have seen how we can check whether two Array instances are identical or not with the help of <=> operator, == operator, and .eql? method? We have also seen different ways through which we can insert elements in the previously defined Array instances. Now, we can say that we have got a decent amount of knowledge about the Array class in Ruby language. In the last article, we have seen the implementation of the assoc() method but the assoc method only works for the Array object which is the collection of multiple Array objects.
在前面的文章中,我們已經看到如何借助<=>運算符 , ==運算符和.eql來檢查兩個Array實例是否相同 。 方法 ? 我們還看到了可以在先前定義的Array實例中插入元素的不同方法。 現在,可以說我們已經對Ruby語言中的Array類有了相當多的了解。 在上一篇文章中,我們看到了assoc()方法的實現,但是assoc方法僅適用于Array對象,后者是多個Array對象的集合。
For normal Array objects, we have Array.include?(obj) method. In this article, we will see how we can implement Array.include?() method? We will go through its syntax and some examples in the rest of the Array.
對于普通的Array對象,我們有Array.include?(obj)方法 。 在本文中,我們將看到如何實現Array.include?()方法 ? 我們將在數組的其余部分中介紹其語法和一些示例。
Method description:
方法說明:
This method is a 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. It will search through the whole Array and gives you the result according to its search. Let us go through the syntax and demonstrating the program codes of this method.
該方法是Public實例方法,屬于Array類,該類位于Ruby語言庫中。 此方法用于檢查對象是否為特定Array實例的一部分。 它將搜索整個數組,并根據其搜索結果。 讓我們來看一下語法,并演示該方法的程序代碼。
If you are thinking about what it will return then let me tell you, it will return a Boolean value. The returned value will be true if it finds the object inside the Array and the return value will be false if it does not find the object to be the part of the Array instance.
如果您正在考慮它將返回什么,那么讓我告訴您,它將返回一個布爾值。 如果在數組內找到對象,則返回值將為true;如果找不到對象是Array實例的一部分,則返回值為false。
Syntax:
句法:
array_instance.include?(obj)
Parameter(s):
參數:
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 include method
=end
# array
array1 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]
# input element to search
puts "Enter the element you want to search"
ele = gets.chomp
# checking
if array1.include?(ele) != false
puts "Element found"
else
puts "Element not found"
end
Output
輸出量
RUN 1:
Enter the element you want to search
Apple
Element found
RUN 2:
Enter the element you want to search
Mango
Element not found
Explanation:
說明:
In the above code, you can observe that we are invoking the include method on the normal Array instance. It has returned true when it found the presence of an object in the Array object which is entered by the user.
在上面的代碼中,您可以觀察到我們在普通Array實例上調用include方法。 當發現用戶輸入的Array對象中存在對象時,它返回true。
Example 2:
范例2:
=begin
Ruby program to demonstrate include method
=end
# arrays
array1 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]
array2 = ["Akul","Madhu","Ashok","Mukesh",788]
array3 = ["Orange","Banana","Papaya","Apricot","Grapes"]
# main array
arraymain = [array1,array2,array3]
# input element to search
puts "Enter the element you want to search"
ele = gets.chomp
# checking
if arraymain.include?(ele) != false
puts "Element found"
else
puts "Element not found"
end
Output
輸出量
Enter the element you want to search
Array1
Element not found
Explanation:
說明:
In the above, you can verify that include method does not work upon Array instance which is the collection of multiple Array instances. It will return false even if the object is a part of the Array instance.
在上面,您可以驗證include方法不適用于Array實例,該實例是多??個Array實例的集合。 即使對象是Array實例的一部分,它也會返回false。
翻譯自: https://www.includehelp.com/ruby/array-include-obj-method-with-example.aspx
obj[]與obj.