ruby array_Ruby中带有示例的Array.values_at()方法

ruby array

Array.values_at()方法 (Array.values_at() Method)

In this article, we will study about Array.values_at() Method. You all must be thinking the method must be doing something which is related to finding values from the Array instance. 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.values_at()方法 。 你们都必须认为该方法必须在做一些与从Array实例中查找值有关的事情。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

Method description:

方法说明:

This method is a public instance method and defined for the Array class in Ruby's library. This method works in a way that it returns an Array instance that contains the object in self corresponding to the given selector. Here selectors refer to the indices you provide to the method at the time of its invocation. These selectors can be either indices or ranges.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是返回一个Array实例,该实例包含与给定选择器相对应的self对象。 在这里,选择器指的是在调用方法时为方法提供的索引。 这些选择器可以是索引或范围。

Syntax:

句法:

    array_instance.values_at(objects,...) -> array

Argument(s) required:

所需参数:

This method can take n number of objects. You can even provide ranges as the argument.

此方法可以使用n个对象。 您甚至可以提供范围作为参数。

Example 1:

范例1:

=begin
  Ruby program to demonstrate values_at method
=end

# array declaration
table = [2,4,8,10,12,134,160,180,200,344]

puts "Array values_at implementation"

puts "Enter the number of indices you want to enter:"
num = gets.chomp.to_i

for i in 1..num
	puts "Enter the index:"
	ele = gets.chomp.to_i
	puts "The value at #{ele} is #{table.values_at(ele)}"
end

Output

输出量

Array values_at implementation
Enter the number of indices you want to enter:
 2
Enter the index:
 0
The value at 0 is [2]
Enter the index:
 1
The value at 1 is [4]

Explanation:

说明:

In the above code, you can observe that we are fetching values from the Array instance with the help of the Array.values_at() method. The method is a returning element whose corresponding index is provided by the user. This method returns an Array that is why a single object is also coming inside the square braces.

在上面的代码中,您可以观察到我们正在借助Array.values_at()方法从Array实例中获取值。 该方法是一个返回元素,其相应索引由用户提供。 此方法返回一个Array,这就是为什么单个对象也进入方括号内的原因。

Example 2:

范例2:

=begin
  Ruby program to demonstrate values_at method
=end

# array declaration
table = [2,4,8,10,12,134,160,180,200,344]

puts "Array values_at implementation"

puts "Enter the lower limit:"
lwr = gets.chomp.to_i
puts "Enter the upper limit:"
upr = gets.chomp.to_i

puts "The objects which lie in the range are : #{table.values_at(lwr..upr)}"

Output

输出量

Array values_at implementation
Enter the lower limit:
 2
Enter the upper limit:
 5
The objects which lie in the range are : [8, 10, 12, 134]

Explanation:

说明:

In the above code, you can observe that we have asked the user for upper and lower limits so that we can pass that range inside the method. In the output, you can see that the Array which is returned by the method is having all the objects which lie in that range.

在上面的代码中,您可以观察到我们已经要求用户提供上限和下限,以便我们可以在方法内部传递该范围。 在输出中,您可以看到该方法返回的Array具有该范围内的所有对象。

翻译自: https://www.includehelp.com/ruby/array-values_at-method-with-example.aspx

ruby array

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值