ruby array_Ruby中带有示例的Array.index()方法
ruby array
Array.index()方法 (Array.index() Method)
In this article, we will study about Array.index() method. You all must be thinking the method must be doing something which is related index of 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.index()方法 。 你們都必須認為方法必須在做某些與某些元素相關的索引的事情。 它并不像看起來那么簡單。 好吧,我們將在其余內容中解決這個問題。 我們將嘗試借助語法并演示程序代碼來理解它。
Method description:
方法說明:
This method is one of the examples of the Public instance method which is specially defined in the Ruby library for Array class. This method is used to find the index of a certain element of the Array instance with the help of the element being passed as the argument. This method works in the way that it returns the index of the first object in Array instance. If you are providing a block instead of an argument then the method will return the index of the first object for which the block returns a Boolean value "True" and returns "nil" if no match is found. This method is one of the examples of non-destructive methods where the changes made by these methods are permanent. There is no destructive version of this method.
此方法是Ruby類庫中為Array類專門定義的Public實例方法的示例之一。 此方法用于在作為參數傳遞的元素的幫助下找到Array實例中某個元素的索引。 此方法以返回Array實例中第一個對象的索引的方式工作。 如果提供的是塊而不是參數,則該方法將返回第一個對象的索引,該對象將為其返回布爾值“ True” ,如果找不到匹配項,則返回“ nil” 。 此方法是非破壞性方法的示例之一,其中通過這些方法所做的更改是永久性的。 此方法沒有破壞性版本。
Syntax:
句法:
array_instance.index()orarray_instance.index(|item| block)Argument(s) required:
所需參數:
This method accepts an argument which should be present in the Array instance. This method returns nil if that element is not present in the Array instance.
此方法接受應在Array實例中出現的參數。 如果Array實例中不存在該元素,則此方法返回nil。
Example 1:
范例1:
=beginRuby program to demonstrate index method =end # array declaration lang = ["C++","Java","Python","Ruby","Perl"]puts "Array index implementation." puts "Enter the element whose index you want to find:" ele = gets.chompif(ind = lang.index(ele))puts "#{ele} found at index #{ind}" elseputs "element is not a part of the Array instance" endOutput
輸出量
Array index implementation. Enter the element whose index you want to find:Ruby Ruby found at index 3Explanation:
說明:
In the above code, you can observe that we are asking the user for the element whose index she wants to find. The user has entered the object "Ruby" which is present in the 3rd index of the Array instance.
在上面的代碼中,您可以觀察到我們正在向用戶詢問要查找其索引的元素。 用戶已經輸入了對象“Ruby”,這是存在于陣列實例的第三索引。
Example 2:
范例2:
=beginRuby program to demonstrate index method =end # array declaration lang = ["C++","Java","Python","Ruby","Perl"]puts "Array index implementation." puts "Enter the element whose index you want to find:" ele = gets.chompif(ind = lang.index{|ind| ind == ele})puts "#{ele} found at index #{ind}" elseputs "element is not a part of the Array instance" endOutput
輸出量
Array index implementation. Enter the element whose index you want to find:Python Python found at index 2Explanation:
說明:
In the above code, you can see that we are passing a block inside the method. The method is returning the index of the first element for which the block stands to be True.
在上面的代碼中,您可以看到我們在方法內部傳遞了一個塊。 該方法返回該塊為True的第一個元素的索引。
翻譯自: https://www.includehelp.com/ruby/array-index-method-with-example.aspx
ruby array
總結
以上是生活随笔為你收集整理的ruby array_Ruby中带有示例的Array.index()方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人类一败涂地做图教程_绘画步骤_人类一败
- 下一篇: dbms_DBMS | 并发控制