array.unshift_Ruby中带有示例的Array.unshift()方法

array.unshift

Array.unshift()方法 (Array.unshift() Method)

In this article, we will study about Array.unshift() Method. You all must be thinking the method must be doing something which is related to unshifting of objects in the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

在本文中,我们将研究Array.unshift()方法 。 你们都必须认为该方法必须执行与Array实例中对象的移位相关的操作。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

Method description:

方法说明:

This method is a public instance method and defined for the Array class in Ruby's library. This method works in a way that affixes the object which is provided with the method at the time of its invocation to the front of the self Array object. It simply means that the object will be shifted to the 0th index and the indices of rest objects are incremented by one. You can provide more than one argument to the method at the time of its invocation. This method is one of the examples of destructive methods where the changes created by the method are permanent. There is no non-destructive version of this method.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是在调用方法时将随方法提供的对象附加到self Array对象的前面。 这仅表示该对象将移至第0个索引,其余对象的索引将增加1。 调用方法时,可以为该方法提供多个参数。 此方法是破坏性方法的示例之一,该方法所创建的更改是永久性的。 没有此方法的非破坏性版本。

Syntax:

句法:

    array_instance.unshift(object) -> array

Argument(s) required:

所需参数:

This method can take n number of objects. You can even provide an Array instance as the argument.

此方法可以使用n个对象。 您甚至可以提供一个Array实例作为参数。

Example 1:

范例1:

=begin
Ruby program to demonstrate unshift method
=end
# array declaration
table = [2,4,8,10,12,134,160,180,200,344]
puts "Array unshift implementation"
puts "Enter the number of objects you want to add:"
num = gets.chomp.to_i
for i in 1..num
puts "Enter the object:"
ele = gets.chomp
table.unshift(ele)
end
puts "The final Array instance: #{table}"

Output

输出量

Array unshift implementation
Enter the number of objects you want to add:
3
Enter the object:
Hrithik
Enter the object:
Amisha
Enter the object:
Satyam
The final Array instance: ["Satyam", "Amisha", "Hrithik", 2, 4, 8, 10, 12, 134, 160, 180, 200, 344]

Explanation:

说明:

In the above code, you can observe that we are adding elements in the Array instance with the help of the Array.unshift() method. In the output, you can see that the objects which are given by the user are stored from 0th index. The previously present elements are moved upward.

在上面的代码中,您可以观察到,借助于Array.unshift()方法 ,我们正在Array实例中添加元素。 在输出中,您可以看到用户指定的对象是从 0 索引开始存储的。 先前存在的元素向上移动。

Example 2:

范例2:

=begin
Ruby program to demonstrate unshift method
=end
# array declaration
table = [2,4,8,10,12,134,160,180,200,344]
puts "Array unshift implementation"
Name = ["Ayush","Saksham","Nikhil"]
table.unshift(Name)
puts "The final Array instance: #{table}"

Output

输出量

Array unshift implementation
The final Array instance: [["Ayush", "Saksham", "Nikhil"], 2, 4, 8, 10, 12, 134, 160, 180, 200, 344]

Explanation:

说明:

In the above code, you can observe that we have prepended an Array instance in the self Array. Now, it has become a subarray of the self Array which is stored at the 0th index of the self Array.

在上面的代码中,您可以观察到我们已经在self Array中添加了Array实例。 现在,它已成为self Array的子​​数组,存储在self Array的第0 索引处。

翻译自: https://www.includehelp.com/ruby/array-unshift-method-with-example.aspx

array.unshift

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

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

相关文章

为什么创建线程池一定要用ThreadPoolExecutor?

作者 | 磊哥来源 | Java面试真题解析(ID:aimianshi666)转载请联系授权(微信ID:GG_Stone)在 Java 语言中,并发编程都是依靠线程池完成的,而线程池的创建方式又有很多,但从…

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

ruby arrayArray.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 …

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)线程安全是指某个方法或某段代码,在多线程中能够正确的执行,不会出现数据不一致或数据污染的…