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

ruby array

Array.fill()方法 (Array.fill() Method)

In this article, we will study about Array.fill() method. You all must be thinking the method must be doing something related to populate the Array instance. Well, we will figure this out in the rest of our content.

在本文中,我们将研究Array.fill()方法 。 你们都必须认为该方法必须做一些与填充Array实例有关的事情。 好吧,我们将在其余内容中解决这个问题。

Method description:

方法说明:

This method is one of the examples of the Public instance method which is specially defined in the Ruby library for Array class. This method is used to populate the Array instances. You can fill multiple objects in the object of the Array class with the help of this method. This method is one of the examples of Destructive methods. This method has many forms and we will be studying them in the rest of the content. There are two of its types are present in this article and are demonstrated with the help of syntaxes and program codes.

此方法是Ruby类库中为Array类专门定义的Public实例方法的示例之一。 此方法用于填充Array实例。 您可以借助此方法在Array类的对象中填充多个对象。 此方法是破坏性方法的示例之一。 这种方法有多种形式,我们将在其余内容中对其进行研究。 本文介绍了它的两种类型,并在语法和程序代码的帮助下进行了演示。

Type 1: fill(obj, range ) -> arr

类型1:fill(obj,range)-> arr

The Array instance will be populated with the object which is passed with the method and the range passed with the method will be deciding from were and up to where the object is going to be stored.

Array实例将使用该方法传递的对象进行填充,并且该方法传递的范围将决定从are到将要存储该对象的位置。

Syntax:

句法:

    array_instance.fill(object,range)

Example 1:

范例1:

=begin
  Ruby program to demonstrate fill method
=end

# array declaration
array1 = ["Kumar","Ramesh","Apple","Pappu","Sana","Yogita","Satyam","Harish"]

puts "Array fill implementation."

puts "Enter the element you want to insert:"
ele = gets.chomp

puts "Enter the start index from where you want to start populating:"
st = gets.chomp.to_i

puts "Enter the last index up to where you want to end populating:"
pp = gets.chomp.to_i

array1.fill(ele,st..pp)

puts "Array elements are:"
puts array1

Output

输出量

Array fill implementation.
Enter the element you want to insert:
 Danish
Enter the start index from where you want to start populating:
 3
Enter the last index up to where you want to end populating:
 5
Array elements are:
Kumar
Ramesh
Apple
Danish
Danish
Danish
Satyam
Harish

Explanation:

说明:

In the above code, you can observe that we have asked the range and object from the user. When the user has entered 3 as the starting index and 5 as the ending index then the Array has been populated with that object at 3rd, 4th and 5th index of the Array instance.

在上面的代码中,您可以观察到我们已经询问了用户的范围和对象。 当用户输入3作为开始索引,输入5作为结束索引时,则在该对象的Array实例的第3,第4和第5个索引处填充了该对象。

Type 2: fill { |index| block } -> arr

类型2:填充{| index | 块}-> arr

This method will not populate the Array instance with the same object. In this method, you will have to pass a block in which you want to process the Array indexes. You can perform any operation of your wish inside the method block.

此方法将不会使用相同的对象填充Array实例。 在这种方法中,您将必须传递一个要在其中处理Array索引的块。 您可以在方法块内执行您希望的任何操作。

Syntax:

句法:

    array_instance.fill { |index| block }

Example 2:

范例2:

=begin
  Ruby program to demonstrate fill method
=end

# array declaration
array1 = ["Kumar","Ramesh","Apple","Pappu","Sana","Yogita","Satyam","Harish"]

puts "Array fill implementation."
array1.fill{|i| i*i}

puts "Array elements are:"
puts array1

Output

输出量

Array fill implementation.
Array elements are:
0
1
4
9
16
25
36
49

Explanation:

说明:

In the above code, you can observe that we have made use of indexes to populate the Array instance. You can see that the Array element is containing the cube of indexes.

在上面的代码中,您可以观察到我们已经利用索引来填充Array实例。 您可以看到Array元素包含索引的多维数据集。

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

ruby array

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值