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:
=beginRuby 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 array1Output
輸出量
Array fill implementation. Enter the element you want to insertvasu Array elements are: vasu vasu vasu vasu vasu vasu vasu vasuExplanation:
說明:
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:
=beginRuby 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.chompputs "From where you want to start populating:" st = gets.chomp.to_iputs "Up to where you want to start populating:" pp = gets.chomp.to_iarray1.fill(ele,st,pp)puts "Array elements are:" puts array1Output
輸出量
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 HarishExplanation:
說明:
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
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的ruby array_Ruby中带有示例的Array.fill()方法(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 方幂序列 c+~+_C ++编程中的Tr
- 下一篇: scala重载无参构造方法_Scala中