日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

obj[]与obj._Ruby中带有示例的Array.include?(obj)方法

發布時間:2025/3/11 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.

在前面的文章中,我們已經看到如何借助<=>運算符 , ==運算符和.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:

=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" end

Output

輸出量

RUN 1: Enter the element you want to searchApple Element foundRUN 2: Enter the element you want to searchMango 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:

=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" end

Output

輸出量

Enter the element you want to searchArray1 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.

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的obj[]与obj._Ruby中带有示例的Array.include?(obj)方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。