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:

=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 start index from where you want to start populating:"
st = gets.chomp.to_i
puts "Enter the number of times"
pp = gets.chomp.to_i
Array1.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 times
2
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.

您可以在上面的示例中观察到,我们在询问用户有关两个输入的信息,第一个是起始索引,第二个是次数。 您可以观察到第二索引上的元素通过某种操作被其索引替换,尽管用户已输入两次,但第三索引上的元素却被对其索引的操作替换。

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:

=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 starting index:"
st = gets.chomp.to_i
puts "Enter the ending index:"
pp = gets.chomp.to_i
array1.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.

在上面的代码中,您可以观察到我们在询问用户第一个索引和最后一个索引。 用户输入了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)相关推荐

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

    ruby array Array.fill()方法 (Array.fill() Method) In this article, we will study about Array.fill() me ...

  2. ruby array_Ruby中带有示例的Array.shuffle方法

    ruby array Array.shuffle方法 (Array.shuffle Method) In this article, we will study about Array.shuffle ...

  3. ruby array_Ruby中带有示例的Array.select方法

    ruby array Array.select方法 (Array.select Method) In the last articles, we have seen how to iterate ov ...

  4. ruby array_Ruby中带有示例的Array.keep_if方法

    ruby array Ruby Array.keep_if方法 (Ruby Array.keep_if Method) In the last articles, we have studied th ...

  5. ruby array_Ruby中带有示例的Array.delete_if方法

    ruby array Ruby Array.delete_if方法 (Ruby Array.delete_if Method) In the last articles, we have studie ...

  6. ruby array_Ruby中带有示例的Array.cycle()方法

    ruby array Array.cycle()方法 (Array.cycle() Method) In this article, we will study about Array.cycle() ...

  7. ruby array_Ruby中带有示例的Array.index()方法

    ruby array Array.index()方法 (Array.index() Method) In this article, we will study about Array.index() ...

  8. ruby array_Ruby中带有示例的Array.sample()方法

    ruby array Array.sample()方法 (Array.sample() Method) In this article, we will study about Array.sampl ...

  9. ruby array_Ruby中带有示例的Array.zip()方法

    ruby array Array.zip()方法 (Array.zip() Method) In this article, we will study about Array.zip() Metho ...

最新文章

  1. 吴恩达Coursera机器学习 - Chapter 4 多变量线性回归
  2. JAVA_OA(五)(番外篇):SpringMVC乱码解决(post,get)
  3. Spark1.x和2.x如何读取和写入csv文件
  4. CNN反向传播卷积核翻转
  5. unit类型是什么?_面试官虚晃一枪:项目中有用过锁吗?能解释一下什么是AQS?...
  6. 又把锅甩给中国!苹果和解给了300多亿,但高通业绩预测并不乐观
  7. 学生表,选课表,课程表
  8. Ipopt安装【Ubuntu18.04】XAVIER(ARM架构)
  9. 23种设计模式之软件设计模式的概念与意义
  10. 打印机驱动兼容问题导致打印乱码
  11. 大数据技术与应用专业 课 程 标 准
  12. 华为彩信显示尚未激活服务器,华为手机无法接收彩信提示要开通gprs
  13. Eggjs笔记:详解Mongoose的聚合管道以及populate实现关联查询
  14. 架构思维成长系列教程
  15. Adaptive Spectrum Noise Cancellation (自适应频谱噪声消除 ,ASNC)去除强运动伪影
  16. qt5 开发及实例(第4版)_才聪学习网_中级微观经济学第4版练习题详解
  17. 面试题之JAVA高级篇
  18. 教你用键盘打出各种符号 如 ♠♣♥........
  19. OSChina 周四乱弹 —— 你妈是洗衣机的亲妈
  20. [搬运]一百三十四 - TikTok原评论区搬运(机翻)

热门文章

  1. 从Maven中央仓库网站下载jar包的两种方式,将会伴随java后端开发者的整个职业生涯
  2. Python报错:PermissionError: [Errno 13] Permission denied 解决方案详解
  3. 腾讯云ubuntu18安装图形化界面
  4. 【数学】Element Swapping
  5. react.js基础
  6. eclipse快捷键、智能提示
  7. PHP-Manual的学习----【语言参考】----【类型】-----【对象】
  8. 斯蒂芬斯蒂芬但是当时发生的s
  9. 桌面虚拟化之用户行为审计
  10. SQL Server 2005 高级程序设计 学习笔记(2)