ruby 执行函数_Ruby at()函数
ruby 執行函數
Ruby中的at()函數 (at() function in Ruby)
If you are working with arrays in Ruby, sometimes you may need to find the element at a particular index. For meeting the purpose, we have got at() function in Ruby which is already defined in Ruby's library. The basic purpose of at() function is to return the element stored at the specified index which is passed as an argument. The index may be positive, negative or zero. If we invoke at() function with 0 index, it will return the first element of the array, likewise, if we invoke at() function with 1 index, it will return the second element of the array. In case of negative indexes, at() with Index -1 will return the last element of the array, at() with Index -2 will return the second last element of the array, at() with Index -3 will return the third last element of the array and so on subject to availability.
如果您在Ruby中使用數組 ,則有時可能需要在特定索引處找到該元素。 為了達到目的,我們在Ruby的庫中已經定義了Ruby中的at()函數 。 at()函數的基本目的是返回存儲在指定索引處的元素,該元素作為參數傳遞。 索引可以為正,負或零。 如果調用索引為0的at()函數 ,它將返回數組的第一個元素,同樣,如果調用索引為1的at()函數 ,它將返回數組的第二個元素。 如果為負索引,則索引為-1的at()將返回數組的最后一個元素,索引為-2的at()將返回數組的第二個元素,索引為-3的at()將返回第三個元素。數組的最后一個元素,依情況而定。
Syntax:
句法:
Array_name.at(Index)Now, let us understand the implementation of at() function with the help of provided examples.
現在,讓我們借助提供的示例了解at()函數的實現。
Example 1:
范例1:
=begin Ruby program to demonstrate implementation of at() function =end# Initialising array of elements Arr = ["C++", "C", "Perl", "Python", "Java", "Visual Basic","C#", "Ruby", "Includehelp"] # Calling to at() function Var1 = Arr.at(0) Var2 = Arr.at(1) Var3 = Arr.at(3) Var4 = Arr.at(5) Var5 = Arr.at(-1) Var6 = Arr.at(-3) # Getting the corresponding elements # whose indexes are given as parameter puts "#{Var1}" puts "#{Var2}" puts "#{Var3}" puts "#{Var4}" puts "#{Var5}" puts "#{Var6}"Output
輸出量
C++ C Python Visual Basic Includehelp C#Code logic:
代碼邏輯:
In the above code, we have initialized an array named 'arr'. We are invoking at() with different indexes as an argument. arr.at(0) is returning the first element of the array and coming towards negative indexes, at(-1) is returning the last element of the array.
在上面的代碼中,我們初始化了一個名為“ arr”的數組。 我們以不同的索引作為參數調用at() 。 arr.at(0)返回數組的第一個元素并指向負索引, at(-1)返回數組的最后一個元素。
Example 2:
范例2:
=begin Ruby program to demonstrate implementation of at() function =end# Initialising array of elements Arr = ["C++", "C", "Perl", "Python", "Java", "Visual Basic","C#", "Ruby", "Includehelp"] #Taking index from user puts "Enter the index of the element you want to search" i = gets.chomp.to_i puts "The element at index #{i} is #{Arr.at(i)}"Output
輸出量
Run 1: Enter the index of the element you want to search 3 The element at index 3 is PythonRun 2: Enter the index of the element you want to search -1 The element at index -1 is IncludehelpCode logic:
代碼邏輯:
You can observe in the above code that we are taking an index as an input from the user and invoking at() function with that index. The function is returning values corresponding to that index.
您可以在上面的代碼中觀察到,我們將索引作為用戶的輸入,并使用該索引調用at()函數 。 該函數正在返回與該索引對應的值。
Example 3:
范例3:
=begin Ruby program to demonstrate implementation of at() function =end# Initialising array of elements Arr = ["C++", "C", "Perl", "Python", "Java", "Visual Basic","C#", "Ruby", "Includehelp"] #Storing value in the variable p=Arr.at(1) q=Arr.at(3) puts p puts qOutput
輸出量
C PythonCode logic:
代碼邏輯:
We are invoking at() function inside variables. The variable is storing the value which is returned from the at() method. The value can be used for further processing.
我們正在調用變量內部的at()函數 。 該變量存儲從at()方法返回的值。 該值可用于進一步處理。
翻譯自: https://www.includehelp.com/ruby/at-function.aspx
ruby 執行函數
總結
以上是生活随笔為你收集整理的ruby 执行函数_Ruby at()函数的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: c语言位运算符的使用_C语言程序使用位运
 - 下一篇: 土豚mysql_树莓派LNMP配置