ruby array_在Ruby中使用Array.new(size){| index |}创建数组

ruby array

In the previous article, we have learned how we can declare an Array class instance with the help of the Array.new(Array) method? You can also notice that in the program codes written to demonstrate all those methods are having Array instances declared with the conventional method such as,

在上一篇文章中,我们学习了如何在Array.new(Array)方法的帮助下声明Array类实例? 您还可以注意到,在编写用于演示所有这些方法的程序代码中,它们具有使用常规方法声明的Array实例,例如,

    array_name = ['ele1', 'ele2', 'ele3', ..., 'eleN']

Now, after reading the previous articles, we have learnt to declare an Array class object in the following ways as well,

现在,在阅读了之前的文章之后,我们还学习了通过以下方式声明Array类对象:

    array_name = Array.[](*args)
    array_name = Array.new(size = 0, obj = nil)
    array_name = Array.new(size){|index| block}

The above is the way we have used it in the last articles. We have learned three ways through which we can declare an Array in Ruby till time. Well, in this article, we will see that how we can declare an Array object with the help of Array.new(size){|index| block} method.

以上是我们在上一篇文章中使用它的方式。 到目前为止,我们已经学习了三种在Ruby中声明数组的方法。 好吧,在本文中,我们将看到如何在Array.new(size){| index |的帮助下声明Array对象 块}方法。

Method description:

方法说明:

This method is a public class method. Earlier Arrays are created by passing an index to them. In the case of this method, the value of each element is generated by passing the index of the element to the provided block and the returned value is stored.

此方法是公共类方法。 较早的数组是通过将索引传递给它们来创建的。 在这种方法的情况下,通过将元素的索引传递到提供的块来生成每个元素的值,并存储返回的值。

Syntax:

句法:

    array_name = Array.new(size){|index| index#operation}

Parameter(s):

参数:

Arguments play a very important role in this method. This method will take the size as the Argument. Whole processing will be done with the help of the provided block.

在这种方法中,参数起着非常重要的作用。 此方法将大小作为参数。 整个处理将在提供的块的帮助下完成。

Example 1:

范例1:

=begin
    Ruby program to demonstrate the 
    Array.new(size){} method
=end

# array declaration
arr = Array.new(5){|index| index*2}

# printing array elements
puts "Array elements are..."
puts arr

Output

输出量

Array elements are...
0
2
4
6
8

Explanation:

说明:

With the help of the above code, you can easily understand the implementation of the Array.new(size) method. We are creating an Array instance named "arr" with the help of this method. We want the Array object to be of size 5 and the elements should be of index multiple of 2. In the output, you can observe that the Array elements are 0, 2, 4, 6 and 8. The first element is 0 because we know that the indexing of an Array instance starts with 0 always. You can take input of the size from the user as well.

借助以上代码,您可以轻松了解Array.new(size)方法的实现 。 借助此方法,我们正在创建一个名为“ arr”的Array实例。 我们希望Array对象的大小为5,元素应为2的索引倍数。在输出中,您可以观察到Array元素为0、2、4、6和8。第一个元素为0,因为我们知道Array实例的索引始终从0开始。 您也可以从用户那里输入尺寸。

Example 2:

范例2:

=begin
    Ruby program to demonstrate 
    the Array.new(size){} method
=end

# array declaration
arr = Array.new(5){|index|}

# printing array elements
puts "Array elements are..."
puts arr

Output

输出量

Array elements are...

Explanation:

说明:

You can observe in the above code that when you are specifying the criteria through which you want to propagate the Array then nil values are assigned for size times. Here, five nil values are provided to the Array.

您可以在上面的代码中观察到,当您指定要通过其传播数组的条件时,将为大小时间分配nil值。 在此,向数组提供了五个nil值。

翻译自: https://www.includehelp.com/ruby/creating-array-with-array-new-size-index.aspx

ruby array

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值