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 Python
Run 2:
Enter the index of the element you want to search
-1
The element at index -1 is Includehelp
Code 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 q
Output
输出量
C
Python
Code 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 执行函数