Ruby - Array#<< 和 Array#push 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10569529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:07:21  来源:igfitidea点击:

Ruby - Difference between Array#<< and Array#push

rubyarraysappend

提问by RavensKrag

From examining the documentation for Ruby 1.9.3, both Array#<< and Array#push were designed to implement appending an element to the end of the current array. However, there seem to be subtle differences between the two.

通过检查 Ruby 1.9.3 的文档,Array#<< 和 Array#push 都旨在实现将元素追加到当前数组的末尾。但是,两者之间似乎存在细微差别。

The one I have encountered is that the * operator can be used to append the contents of an entire other array to the current one, but only with #push.

我遇到的一个是 * 运算符可用于将整个其他数组的内容附加到当前数组,但只能使用 #push。

a = [1,2,3]
b = [4,5,6]

a.push *b
=> [1,2,3,4,5,6]

Attempting to use #<< instead gives various errors, depending on whether it's used with the dot operator and/or parentheses.

尝试使用 #<< 会产生各种错误,具体取决于它是否与点运算符和/或括号一起使用。

Why does #<< not work the same way #push does? Is one not actually an alias for the other?

为什么#<< 与#push 的工作方式不同?一个实际上不是另一个的别名吗?

回答by x1a4

They are very similar, but not identical.

它们非常相似,但并不完全相同。

<<accepts a single argument, and pushes it onto the end of the array.

<<接受单个参数,并将其推送到数组的末尾。

push, on the other hand, accepts one or more arguments, pushing them all onto the end.

push,另一方面,接受一个或多个参数,将它们全部推到最后。

The fact that <<only accepts a single object is why you're seeing the error.

<<仅接受单个对象的事实就是您看到错误的原因。

回答by David Weiser

The reason why <<does not work and pushdoes is that:

<<行不通的原因push是:

  1. pushcan accept many arguments (which is what happens when you do *b).
  2. <<only accepts a single argument.
  1. push可以接受许多参数(当你这样做时会发生什么*b)。
  2. <<只接受一个参数。

回答by Akshay Mohite

The main difference between Array#<< and Array#push is

Array#<< 和 Array#push 的主要区别是

Array#<< # can be used to insert only single element in the Array

Array#push # can be used to insert more than single element in the Array

Another significant difference is, In case of inserting single element,

另一个显着区别是,在插入单个元素的情况下,

Array#<< is faster than Array#push

Array#<< 比 Array#push 快

Benchmarkingcan help in finding out the performance of these two ways, find more here.

基准测试可以帮助找出这两种方式的性能,请在此处找到更多信息

回答by Santhosh

Another important point to note here is that <<is also an operator. And it has a lower precedence than some operators like the ternary operator. This might lead to some unexpected results. eg:

这里要注意的另一个重点是这<<也是一个运算符。并且它的优先级低于一些运算符,例如三元运算符。这可能会导致一些意想不到的结果。例如:

arr1, arr2 = [], []

arr1.push true ? 1 : 0
arr1
# => [1] 

arr2 << true ? 1 : 0
arr2
# => [true] 

回答by Abhishek Singh

The pushmethod appends an item to the end of the array.It can have more than one argument. <<is used to initialize array and can have only one argument , adds an element at the end of array if already initialized.

push方法将一项附加到数组的末尾。它可以有多个参数。 <<用于初始化数组,只能有一个参数,如果已经初始化,则在数组末尾添加一个元素。