obj[]与obj._Ruby中带有示例的Array.include?(obj)方法
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.
在前面的文章中,我們已經(jīng)看到如何借助<=>運(yùn)算符 , ==運(yùn)算符和.eql來檢查兩個(gè)Array實(shí)例是否相同 。 方法 ? 我們還看到了可以在先前定義的Array實(shí)例中插入元素的不同方法。 現(xiàn)在,可以說我們已經(jīng)對(duì)Ruby語言中的Array類有了相當(dāng)多的了解。 在上一篇文章中,我們看到了assoc()方法的實(shí)現(xiàn),但是assoc方法僅適用于Array對(duì)象,后者是多個(gè)Array對(duì)象的集合。
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.
對(duì)于普通的Array對(duì)象,我們有Array.include?(obj)方法 。 在本文中,我們將看到如何實(shí)現(xiàn)Array.include?()方法 ? 我們將在數(shù)組的其余部分中介紹其語法和一些示例。
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實(shí)例方法,屬于Array類,該類位于Ruby語言庫中。 此方法用于檢查對(duì)象是否為特定Array實(shí)例的一部分。 它將搜索整個(gè)數(shù)組,并根據(jù)其搜索結(jié)果。 讓我們來看一下語法,并演示該方法的程序代碼。
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.
如果您正在考慮它將返回什么,那么讓我告訴您,它將返回一個(gè)布爾值。 如果在數(shù)組內(nèi)找到對(duì)象,則返回值將為true;如果找不到對(duì)象是Array實(shí)例的一部分,則返回值為false。
Syntax:
句法:
array_instance.include?(obj)Parameter(s):
參數(shù):
This method only takes one parameter and that argument is nothing but an object whose presence we want to check.
此方法僅使用一個(gè)參數(shù),而該參數(shù)不過是一個(gè)要檢查其存在性的對(duì)象。
Example 1:
范例1:
=beginRuby 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) != falseputs "Element found" elseputs "Element not found" endOutput
輸出量
RUN 1: Enter the element you want to searchApple Element foundRUN 2: Enter the element you want to searchMango Element not foundExplanation:
說明:
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.
在上面的代碼中,您可以觀察到我們?cè)谄胀ˋrray實(shí)例上調(diào)用include方法。 當(dāng)發(fā)現(xiàn)用戶輸入的Array對(duì)象中存在對(duì)象時(shí),它返回true。
Example 2:
范例2:
=beginRuby 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) != falseputs "Element found" elseputs "Element not found" endOutput
輸出量
Enter the element you want to searchArray1 Element not foundExplanation:
說明:
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.
在上面,您可以驗(yàn)證include方法不適用于Array實(shí)例,該實(shí)例是多??個(gè)Array實(shí)例的集合。 即使對(duì)象是Array實(shí)例的一部分,它也會(huì)返回false。
翻譯自: https://www.includehelp.com/ruby/array-include-obj-method-with-example.aspx
obj[]與obj.
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的obj[]与obj._Ruby中带有示例的Array.include?(obj)方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java uuid静态方法_Java U
- 下一篇: BE的完整形式是什么?