ruby array_Ruby中带有示例的Array.fill()方法(3)
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(start [, length] ) { |index| block } -> arr
類型1:fill(start [,length]){| index | 塊}-> arr
The Array instance will be populated with the indexes and you can manipulate the index as per your requirements.
Array實例將使用索引填充,您可以根據需要操作索引。
Syntax:
句法:
array_instance.fill(start [, length] ) { |index| block }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 start index from where you want to start populating:" st = gets.chomp.to_iputs "Enter the number of times" pp = gets.chomp.to_iArray1.fill(st,pp){|i| i*i}puts "Array elements are:" puts Array1Output
輸出量
Array fill implementation. Enter the start index from where you want to start populating:2 Enter the number of times2 Array elements are: Kumar Ramesh 4 9 Sana Yogita Satyam HarishExplanation:
說明:
You can observe in the above example that we are asking the user about two inputs first one is the starting index and another one is the number of times. You can observe that the element on the 2nd index is replaced by its index with some manipulation and though the user has entered two times then the element on the 3rd index is replaced by the manipulation of its index.
您可以在上面的示例中觀察到,我們在詢問用戶有關兩個輸入的信息,第一個是起始索引,第二個是次數。 您可以觀察到第二索引上的元素通過某種操作被其索引替換,盡管用戶已輸入兩次,但第三索引上的元素卻被對其索引的操作替換。
Type 2: fill(range){|index|block }
類型2:填充(范圍){| index | block}
The Array instance will be populated with the indexes and you can manipulate the index as per your requirements. You have to provide the range from where and up to where you want to populate the Array instance with indexes.
Array實例將使用索引填充,您可以根據需要操作索引。 您必須提供從何處到要用索引填充Array實例的范圍。
Syntax:
句法:
array_instance.fill(range){|index| block}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 starting index:" st = gets.chomp.to_iputs "Enter the ending index:" pp = gets.chomp.to_iarray1.fill(st..pp){|i| i*i}puts "Array elements are:" puts array1Output
輸出量
Array fill implementation. Enter the starting index:2 Enter the ending index:4 Array elements are: Kumar Ramesh 4 9 16 Yogita Satyam HarishExplanation:
說明:
In the above code, you can observe that we are asking the user for the first index and last index. The user has entered 2 as the starting index and 4 is the ending index. So, you can observe that on the 2nd, 3rd and 4th index, the elements are overwritten with the index manipulation.
在上面的代碼中,您可以觀察到我們在詢問用戶第一個索引和最后一個索引。 用戶輸入了2作為開始索引,輸入4是結束索引。 所以,你可以觀察到,在第2 次 ,第3 次和第 4 個索引,所述元件與所述索引操作覆蓋。
翻譯自: https://www.includehelp.com/ruby/array-fill-method-with-example-3.aspx
ruby array
總結
以上是生活随笔為你收集整理的ruby array_Ruby中带有示例的Array.fill()方法(3)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css clearfix_如何使用CSS
- 下一篇: 软件测试 测试策略_测试策略| 软件工程