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

ruby array

Array.take()方法 (Array.take() Method)

In this article, we will study about Array.take() Method. You all must be thinking the method must be doing something related to taking elements or objects 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.take()方法 。 你们都必须认为该方法必须在做一些与从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 takes n number of arguments from the Array instance and returns a new Array instance that contains the objects which are being taken from the self Array. The method will throw an exception if you provide a negative value along with the method at the time of its invocation. This method is one of the examples of the non-destructive method where the method does not bring any changes in the actual arrangement of objects in the self Array.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是从Array实例中获取n个参数,然后返回一个新的Array实例,其中包含从self Array中获取的对象。 如果在调用方法的同时提供负值,则该方法将引发异常。 此方法是非破坏性方法的示例之一,该方法不会对self Array中的对象的实际排列带来任何更改。

Syntax:

句法:

    array_instance.take(n) -> new_array or nil

Argument(s) required:

所需参数:

This method requires only one argument which decides the number of objects to be taken from the Array instance. The argument strictly needs to be a positive integer otherwise the method will throw an exception.

此方法仅需要一个参数即可确定要从Array实例获取的对象数。 参数必须严格为正整数,否则该方法将引发异常。

Example 1:

范例1:

=begin
  Ruby program to demonstrate take method
=end

# array declaration
table = [2,4,6,8,10,12,14,16,18,20]

puts "Array take implementation"

puts "Enter the number of objects you want to take from the Array:"
num = gets.chomp.to_i

if(table.take(num))
	puts "The objects taken are: #{table.take(num)}"
else
	puts "Exception occured"
end

puts "Array instance: #{table}"

Output

输出量

RUN 1:
Array take implementation
Enter the number of objects you want to take from the Array:
 4
The objects taken are: [2, 4, 6, 8]
Array instance: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

RUN 2:
Array take implementation
Enter the number of objects you want to take from the Array:
 -5
attempt to take negative size
(repl):13:in `take'
(repl):13:in `<main>'

Explanation:

说明:

In the above code, you can observe that we are taking the elements from the Array instance with the help of the Array.take() method. The taking of objects or elements will start from the very first index of the Array instance which is always 0. In the second output, you can observe that when we are providing a negative index, the method is throwing an exception because in the case of this method there is nothing like objects can be taken from the end of the Array instance.

在上面的代码中,您可以观察到我们是在Array.take()方法的帮助下从Array实例中获取元素的 对象或元素的获取将从始终为0的Array实例的第一个索引开始。在第二个输出中,您可以观察到,当我们提供负索引时,该方法将引发异常,因为在这种情况下,此方法没有什么可以像从Array实例的末尾获取对象一样。

Example 2:

范例2:

=begin
  Ruby program to demonstrate take method
=end

# array declaration
table = ["Subha","Sham","Raat","Vivek","Me"]

puts "Array take implementation"

puts "Enter the number of objects you want to take from the Array:"
num = gets.chomp.to_i

if(table.take(num))
	puts "The objects taken are: #{table.take(num)}"
else
	puts "Exception occured"
end

puts "Array instance: #{table}"

Output

输出量

RUN 1:
Array take implementation
Enter the number of objects you want to take from the Array:
 3
The objects taken are: ["Subha", "Sham", "Raat"]
Array instance: ["Subha", "Sham", "Raat", "Vivek", "Me"]

RUN 2:
Array take implementation
Enter the number of objects you want to take from the Array:
 -9
attempt to take negative size
(repl):13:in `take'
(repl):13:in `<main>'

Explanation:

说明:

This program is demonstrated just to show you that this method works on String Array as well. In the above code, you can observe that we are taking the elements from the Array instance with the help of the Array.take() method. The taking of objects or elements will start from the very first index of the Array instance which is always 0. In the second output, you can observe that when we are providing a negative index, the method is throwing an exception because in the case of this method there is nothing like objects can be taken from the end of the Array instance.

演示该程序只是为了向您展示该方法也适用于String Array。 在上面的代码中,您可以观察到我们借助于Array.take()方法从Array实例中获取元素。 对象或元素的获取将从始终为0的Array实例的第一个索引开始。在第二个输出中,您可以观察到,当我们提供负索引时,该方法将引发异常,因为在这种情况下,此方法没有什么可以像从Array实例的末尾获取对象一样。

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

ruby array

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值