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:

=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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544243.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Objects.equals有坑

前言最近review别人代码的时候,发现有个同事,在某个业务场景下,使用Objects.equals方法判断两个值相等时,返回了跟预期不一致的结果,引起了我的兴趣。原本以为判断结果会返回true的,但实际上返回了false。记…

strtoupper 小写_PHP strtoupper()函数与示例

strtoupper 小写PHP strtoupper()函数 (PHP strtoupper() function) strtoupper() function is a string function it accepts the string and returns an uppercase string. strtoupper()函数是一个字符串函数,它接受字符串并返回大写字符串。 Syntax: 句法&#…

Java 18 正式发布,默认 UTF-8,finalize 被弃用,别再乱用了!

JDK 18 正式发布JDK 17 刚发布半年,JDK 18 又如期而至,JDK 版本号这算是成年了?JDK 18 发布了,栈长继续为大家解读!JDK 18 延续了 JDK 17 开创的免费策略,但,JDK 18~20 不是长期支持…

Spring官方推荐的@Transactional还能导致生产事故?

在Spring中进行事务管理非常简单,只需要在方法上加上注解Transactional,Spring就可以自动帮我们进行事务的开启、提交、回滚操作。甚至很多人心里已经将Spring事务与Transactional划上了等号,只要有数据库相关操作就直接给方法加上Transactio…

python函数实例化_用Python实例化函数

python函数实例化In terms of Mathematics and Computer science, currying is the approach/technique by which we can break multiple-argument function to single argument function. 从数学和计算机科学的角度来看, 柯里化是一种方法/技术,通过它我…

京东二面:MySQL 主从延迟、读写分离 7 种解决方案!

我们都知道互联网数据有个特性,大部分场景都是 读多写少,比如:微博、微信、淘宝电商,按照 二八原则,读流量占比甚至能达到 90%结合这个特性,我们对底层的数据库架构也会做相应调整。采用 读写分离处理过程&…

array_keys_PHP array_keys()函数与示例

array_keysPHP array_keys()函数 (PHP array_keys() function) array_keys() function is used to get the keys of an array, it accepts an array as an argument and returns a new array containing keys. array_keys()函数用于获取数组的键,它接受一个数组作为…

再见Postman,这款API神器更好用!

代码未动,文档先行其实大家都知道 API 文档先行的重要性,但是在实践过程中往往会遇到很多困难。程序员最讨厌的两件事:1. 写文档,2. 别人不写文档。大多数开发人员不愿意写 API 文档的原因是写文档短期收益远低于付出的成本&#…

strtolower_PHP strtolower()函数与示例

strtolowerPHP strtolower()函数 (PHP strtolower() function) strtolower() function is a string function, it accepts the string and returns an lowercase string. strtolower()函数是一个字符串函数,它接受该字符串并返回小写字符串。 Syntax: 句法&#xf…

如何保证数据库和缓存双写一致性?

前言数据库和缓存(比如:redis)双写数据一致性问题,是一个跟开发语言无关的公共问题。尤其在高并发的场景下,这个问题变得更加严重。我很负责的告诉大家,该问题无论在面试,还是工作中遇到的概率非…

面试官:AtomicInteger是如何保证线程安全?

blog.csdn.net/nanhuaibeian/article/details/120936139一、为什么引入 AtomicInteger ?谈到线程安全,会首先想到了synchronized 和 Lock,但是这种方式又有一个名字,叫做互斥锁,一次只能有一个持有锁的线程进入,再加上…

机器学习 训练验证测试_测试前验证| 机器学习

机器学习 训练验证测试In my previous article, we have discussed about the need to train and test our model and we wrote a code to split the given data into training and test sets. 在上一篇文章中,我们讨论了训练和测试模型的必要性,并编写了…

如何判断线程池已经执行完所有任务了?

作者 | 磊哥来源 | Java面试真题解析(ID:aimianshi666)转载请联系授权(微信ID:GG_Stone)很多场景下,我们需要等待线程池的所有任务都执行完,然后再进行下一步操作。对于线程 Thread …

IRCTC的完整形式是什么?

IRCTC:印度铁路餐饮和旅游公司 (IRCTC: Indian Railways Catering and Tourism Corporation) IRCTC is an abbreviation of Indian Railways Catering and Tourism Corporation. It is a subsidiary of the Indian Railway established by the Ministry of Railways…

分布式锁的 3 种实现方案!

前言 大家好,我是磊哥。今天跟大家探讨一下分布式锁的设计与实现。希望对大家有帮助,如果有不正确的地方,欢迎指出,一起学习,一起进步哈~分布式锁概述数据库分布式锁Redis分布式锁Zookeeper分布式锁三种分布式锁对比1.…

java学习笔记16--异常

java学习笔记16--异常 异常 异常时导致程序中断运行的一种指令流,如果不对异常进行正确的处理,则可能导致程序的中断执行,造成不必要的损失, 所以在程序的设计中必须要考虑各种异常的发生,并正确的做好相应的处理&am…

ruby hash添加数据_如何在Ruby中向Hash添加元素?

ruby hash添加数据Before going through the ways to add elements to the hash instances, let us understand what could be called as a hash element. So, Hash is the collection of keys and their values. For example, 在介绍向哈希实例添加元素的方法之前,…

线程安全问题的 3 种解决方案!

作者 | 磊哥来源 | Java面试真题解析(ID:aimianshi666)转载请联系授权(微信ID:GG_Stone)线程安全是指某个方法或某段代码,在多线程中能够正确的执行,不会出现数据不一致或数据污染的…

黑色30s高并发IIS设置

在这篇博文中,我们抛开对阿里云的怀疑,完全从ASP.NET的角度进行分析,看能不能找到针对问题现象的更合理的解释。 “黑色30秒”问题现象的主要特征是:排队的请求(Requests Queued)突增,到达HTTP.…

我们可以覆盖Java中的main()方法吗?

The question is that "Can we override main() method in Java?" 问题是“我们可以覆盖Java中的main()方法吗?” No, we cant override the main() method in java. 不,我们不能覆盖java中的main()方法 。 First, we will understand what …