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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541137.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541137.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541137.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python二分法查找程序_Python程序查找最大EVEN數

python二分法查找程序Input N integer numbers and we have to find the maximum even number. 輸入N個整數,我們必須找到最大的偶數。 There are many ways of doing this but this time, we have to thought of most computationally efficient algorithm to do …

如何快速精確的和leader溝通

2019獨角獸企業重金招聘Python工程師標準>>> 【緣起】 一個同學找我討論個事情,溝通了一會還是不確定要表達什么,希望我配合什么。結合自己的經驗,簡單的聊聊“如何快速精準的和leader溝通一件事”。 【員工角度的潛在困惑&#x…

java字符串最長回文串_Java中的字符串回文程序

java字符串最長回文串Given a string and we have to check whether it is palindrome string or not. 給定一個字符串,我們必須檢查它是否是回文字符串。 A string that is equal to its reverse string is known as palindrome string. To implement the program…

UOJ#31 【UR #2】豬豬俠再戰括號序列

傳送門http://uoj.ac/problem/31 大家好我是來自百度貼吧的_叫我豬豬俠,英文名叫_CallMeGGBond。 我不曾上過大學,但這不影響我對離散數學、復雜性分析等領域的興趣;尤其是括號序列理論,一度令我沉浸其中,無法自拔。至…

li怎么讓文字在圖片下面_div+css(ul li)實現圖片上文字下列表布局

css樣式表代碼:html布局代碼:效果圖:html布局部分,可根據自己需要添加對應的div即可。1、CSS關鍵樣式單詞解釋1)、ul.imglist{ margin:0 auto; width:536px; overflow:hidden}使用margin:0 auto,讓ul結構布局居中&…

如何使用React Native樣式表?

Without wasting much time, a style sheet as commonly known in a CSS is an object or block of code of many styling properties and values which is applied in a code when called. 在不浪費大量時間的情況下,CSS中通常已知的樣式表是具有許多樣式屬性和值的…

【iCore1S 雙核心板_ARM】例程三:EXTI中斷輸入實驗——讀取ARM按鍵狀態

實驗原理: 按鍵的一端與STM32的GPIO(PB9)相連,且PB9外接一個1k大小的限流上接電阻。 初始化時把PB9設置成輸入模式,當按鍵彈起時,PB9由于上拉電阻的作用呈高電平(3.3V); 當按鍵按下時&#xff0…

MySQL小黑框怎么打開_打開你的小黑框命令行,來跟我一起嗨嗨嗨

文章更新于2020-03-16關于電腦位數:位數代表cpu可尋址的內存地址大小。32位的cpu最多可使用4GB內存,而64位cpu能處理的內存范圍就高多了。操作系統也類似,只要看到操作系統里面能識別8GB內存就可以知道cpu和操作系統都是64位。一、常用的 cmd…

您如何從Python的stdin中讀取信息?

Python supports following ways to read an input from stdin (standard input), Python支持以下方式從stdin(標準輸入)讀取輸入 , 1)使用sys.stdin (1) Using sys.stdin) sys.stdin is a file-like object on which we can call functions read() or readlines()…

CentOS7下的AIDE***檢測配置

1、AIDE的簡單介紹AIDE通過掃描一臺(未被篡改)的Linux服務器的文件系統來構建文件屬性數據庫,以后將服務器文件屬性與數據庫中的進行校對,然后在服務器運行時對被修改的索引了的文件發出警告。出于這個原因,AIDE必須在…

mysql主從不同步 tar_Mysql主從不同步問題處理案例

在使用Mysql的主從復制架構中,有兩個比較頭疼的問題:1、主從數據不同步后如何處理2、主從同步延遲問題如何解決本文將根據實際案例來分析下問題1,至于問題2多數文檔介紹的辦法是啟用多線程復制來解決,言歸正傳,這里的問…

編程語言優缺點_R編程語言的優缺點

編程語言優缺點In general, the R programming language is considered as the machine learning language. This is widely employed in the applications where the data analysis, visualization, and the sampling process are involved. The R programming language is ta…

mysql重做日志與binlog日志區別_MySQL中的重做日志(redo log),回滾日志(undo log),以及二進制日志(binlog)的簡單總結...

MySQL中有六種日志文件,分別是重做日志(redo log)回滾日志(undo log)二進制日志(binlog)錯誤日志(errorlog)慢查詢日志(slow query log)一般查詢日志(general log)中繼日志(relay log)。其中重做日志和回滾日志與事務操作息息相關,二進制日志也與事務操作…

python 繪制三角函數_Python | 繪制三角函數

python 繪制三角函數Trigonometry is one of the most important parts in engineering and many times, therefore matplotlib.pyplot in combination with NumPy can help us to plot our desired trigonometric functions. In this article, we are going to introduce a fe…

《深入理解Elasticsearch(原書第2版)》一2.3.3 把查詢模板保存到文件

本節書摘來華章計算機《深入理解Elasticsearch(原書第2版)》一書中的第2章 ,第2.3.3節,[美]拉斐爾酷奇(Rafal Ku) 馬雷克羅戈任斯基(Marek Rogoziski)著 張世武 余洪淼 商旦 譯 …

python兩個中文隊列比較_具有兩個優先級的優先級隊列Python

使用NPE的策略-一個tuple作為隊列優先級,tuple是(fpriority, spriority):import Queueclass Job(object):def __init__(self, fpriority, spriority, descriptionblah, iatafoo , hopsample, costfree pitchers):self.fpriority fpriorityself.spriorit…

之江學院第0屆 A qwb與支教 容斥與二分

題目鏈接: http://115.231.222.240:8081/JudgeOnline/problem.php?cid1005&pid0 題目描述: 給你三個數x, y, z 和 N 輸出從1開始數第N個不是x, y, z 任意一個數的倍數的數字 解題思路: 一看到倍數我先想到素數唯一分解定理, …

java toarray_Java Vector toArray()方法與示例

java toarray向量類toArray()方法 (Vector Class toArray() method) Syntax: 句法: public Object[] toArray();public Object[] toArray(Type[] ty);toArray() method is available in java.util package. toArray()方法在java.util包中可用。 toArray() method i…

Python基礎--環境配置、編碼風格、基礎概念、基本數據類型(1)

#######python########python的基本[rootdesktop ~]# yum install python -y[rootdesktop ~]# python -V ##查看python版本Python 2.7.5[rootdesktop ~]# python --versionPython 2.7.5為什么用/usr/bin/python關于python腳本中的第一行內容 :#!/usr/bin/python 這種寫法表示…

java treemap_Java TreeMap keySet()方法與示例

java treemapTreeMap類的keySet()方法 (TreeMap Class keySet() method) keySet() method is available in java.util package. keySet()方法在java.util包中可用。 keySet() method is used to return a set of keys that exists in this TreeMap to be viewed in a Set. keyS…