Ruby 数组创建,Array.new 与 []

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4601652/
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-03 00:00:43  来源:igfitidea点击:

Ruby array creation, Array.new vs []

ruby-on-railsruby

提问by Lan

What's the difference between these two statements? I use them in my rails app and to me it looks like they do the same thing

这两种说法有什么区别?我在我的 rails 应用程序中使用它们,对我来说看起来它们做同样的事情

array_a = Array.new
array_b = []

回答by zetetic

Those two statements are functionally identical. Array.newhowever can take arguments and a block:

这两个语句在功能上是相同的。Array.new但是可以带参数和块:

Array.new # => []
Array.new(2) # => [nil,nil]
Array.new(5,"A") # =>["A","A","A","A","A"]

a = Array.new(2,Hash.new)
a[0]['cat'] = 'feline'
a # => [{"cat"=>"feline"},{"cat"=>"feline"}]
a[1]['cat'] = 'Felix'
a # => [{"cat"=>"Felix"},{"cat"=>"Felix"}]

a = Array.new(2){Hash.new} # Multiple instances
a[0]['cat'] = 'feline'
a # =>[{"cat"=>"feline"},{}]
squares = Array.new(5){|i|i*i}
squares # => [0,1,4,9,16]

copy = Array.new(squares) # initialized by copying
squares[5] = 25
squares # => [0,1,4,9,16,25]
copy # => [0,1,4,9,16]

Note: the above examples taken from Programming Ruby 1.9

注意:以上示例摘自Programming Ruby 1.9

回答by stef

[]is a shortcut to the Array class's singleton method []which in turn creates a new Array in just the same way as Array.new, so you could probably say 'they are the same' without worrying too much.

[]是 Array 类的单例方法的快捷方式,[]它反过来以与 相同的方式创建一个新的 Array Array.new,因此您可能会说“它们是相同的”而不必担心太多。

Note that each call to []in irb creates a new Array:

请注意,每次调用[]in irb 都会创建一个新数组:

>> [].object_id
=> 2148067340
>> [].object_id
=> 2149414040

From Ruby's C code:

来自Ruby 的 C 代码

rb_define_singleton_method(rb_cArray, "[]", rb_ary_s_create, -1);

回答by 02040402

Such as Hash.newvs {}. They are the same. Include speed.

比如Hash.newvs {}。他们是一样的。包括速度。

回答by sethvargo

There is fundamentally no difference

基本没有区别

回答by Lucas Emerick

There is no difference, but...

没有区别,但是...

As others have already answered you

正如其他人已经回答了你

Those two statements are functionally identical

这两个语句在功能上是相同的

But there are guidelines to orient when you should use each one (so your code is easier to read). The reason behind that is:

但是,当您应该使用每一个时,都有指导方针(这样您的代码更易于阅读)。这背后的原因是:

Programs must be written for people to read, and only incidentally for machines to execute.

程序必须是为人们阅读而编写的,而只是偶然地为机器执行而编写。

from: https://github.com/rubocop-hq/ruby-style-guide#literal-array-hash

来自:https: //github.com/rubocop-hq/ruby-style-guide#literal-array-hash

Prefer literal array and hash creation notation (unless you need to pass parameters to their constructors, that is).

首选文字数组和哈希创建符号(除非您需要将参数传递给它们的构造函数)。

So if you are creating an empty array []is the best option, but if you need to create your array with a set of N nil objects, than Array.new(N)is what you should write.

因此,如果您正在创建一个空数组[]是最好的选择,但是如果您需要使用一组 N 零对象创建您的数组,Array.new(N)那么您应该编写。