ruby array_Ruby中带有示例的Array.fill()方法(1)
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) -> arr
类型1:填充(obj)-> arr
The Array instance will be populated with the object which is passed with the method.
Array实例将使用该方法传递的对象填充。
Syntax:
句法:
array_instance.fill(object)
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
array1.fill(ele)
puts "Array elements are:"
puts array1
Output
输出量
Array fill implementation.
Enter the element you want to insert
vasu
Array elements are:
vasu
vasu
vasu
vasu
vasu
vasu
vasu
vasu
Explanation:
说明:
You can observe in the above example that when the object is passed with the method then it has overwritten all the elements present in the Array instances which we stored at the time of declaration of the Array instance.
您可以在上面的示例中观察到,当对象与方法一起传递时,该对象将覆盖Array实例中存在的所有元素,这些元素在声明Array实例时存储。
Type 2: fill(obj, start [, length])
类型2:fill(obj,start [,length])
This method will not populate the Array instance with the same object. In this method, you will have to pass the object along with the index from where you want to insert the element and up to where you want to populate the Array instance with the same object.
此方法将不会使用相同的对象填充Array实例。 在此方法中,您将必须将对象与索引一起从您要插入元素的位置传递到您要使用相同对象填充Array实例的位置。
Syntax:
句法:
array_instance.fill(obj,start[,length])
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."
puts "Enter the element you want to insert:"
ele = gets.chomp
puts "From where you want to start populating:"
st = gets.chomp.to_i
puts "Up to where you want to start 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:
Amisha
From where you want to start populating:
2
Up to where you want to start populating:
4
Array elements are:
Kumar
Ramesh
Amisha
Amisha
Amisha
Amisha
Satyam
Harish
Explanation:
说明:
In the above code, you can observe that we are asking the user for the object, form, and length. The user has entered 3 as the starting index and 3 is the number of repetitions of the object. So, you can observe that the object has been inserted at the 3rd index and has been repeated for three times by overwriting the already present elements of the object of Array class.
在上面的代码中,您可以观察到我们正在向用户询问对象,形式和长度。 用户已输入3作为起始索引,并且3是对象的重复次数。 因此,您可以观察到该对象已插入第3个索引,并且通过覆盖Array类的对象已存在的元素而重复了3次。
翻译自: https://www.includehelp.com/ruby/array-fill-method-with-example-1.aspx
ruby array
上一篇: es6数组之fill()
推荐阅读
-
ruby array_Ruby中带有示例的Array.select方法
-
ruby array_Ruby中带有示例的Array.shuffle方法
-
ruby array_Ruby中带有示例的Array.cycle()方法
-
ruby .each_Ruby中带有示例的Array.each_index方法
-
Ruby中带有示例的Array.collect方法
-
ruby array_Ruby中带有示例的Array.drop_while方法
-
ruby中、.reject_Ruby中带有示例的Array.reject方法
-
flatten ruby_Ruby中带有示例的Array.flatten方法
-
ruby array_Ruby中带有示例的Array.rotate()方法
-
ruby array_Ruby中带有示例的Array.fill()方法(1)