日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/11 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby array_Ruby中带有示例的Array.fill()方法(3) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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()方法 。 你們都必須認(rèn)為該方法必須做一些與填充Array實(shí)例有關(guān)的事情。 好吧,我們將在其余內(nèi)容中解決這個(gè)問題。

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實(shí)例方法的示例之一。 此方法用于填充Array實(shí)例。 您可以借助此方法在Array類的對(duì)象中填充多個(gè)對(duì)象。 此方法是破壞性方法的示例之一。 這種方法有多種形式,我們將在其余內(nèi)容中對(duì)其進(jìn)行研究。 本文介紹了它的兩種類型,并在語法和程序代碼的幫助下進(jìn)行了演示。

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實(shí)例將使用索引填充,您可以根據(jù)需要操作索引。

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 Array1

Output

輸出量

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 Harish

Explanation:

說明:

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.

您可以在上面的示例中觀察到,我們?cè)谠儐栍脩粲嘘P(guān)兩個(gè)輸入的信息,第一個(gè)是起始索引,第二個(gè)是次數(shù)。 您可以觀察到第二索引上的元素通過某種操作被其索引替換,盡管用戶已輸入兩次,但第三索引上的元素卻被對(duì)其索引的操作替換。

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實(shí)例將使用索引填充,您可以根據(jù)需要操作索引。 您必須提供從何處到要用索引填充Array實(shí)例的范圍。

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 array1

Output

輸出量

Array fill implementation. Enter the starting index:2 Enter the ending index:4 Array elements are: Kumar Ramesh 4 9 16 Yogita Satyam Harish

Explanation:

說明:

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.

在上面的代碼中,您可以觀察到我們?cè)谠儐栍脩舻谝粋€(gè)索引和最后一個(gè)索引。 用戶輸入了2作為開始索引,輸入4是結(jié)束索引。 所以,你可以觀察到,在第2 ,第3 4 個(gè)索引,所述元件與所述索引操作覆蓋。

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

ruby array

總結(jié)

以上是生活随笔為你收集整理的ruby array_Ruby中带有示例的Array.fill()方法(3)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。