ruby to_a 和 to_ary 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9467395/
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
What's the difference between to_a and to_ary?
提问by hey mike
What's the difference between to_aand to_ary?
to_a和 和有to_ary什么区别?
回答by Matheus Moreira
to_aryis used for implicitconversions, while to_ais used for explictconversions.
to_ary用于隐式转换,而to_a用于显式转换。
For example:
例如:
class Coordinates
attr_accessor :x, :y
def initialize(x, y); @x, @y = x, y end
def to_a; puts 'to_a called'; [x, y] end
def to_ary; puts 'to_ary called'; [x, y] end
def to_s; "(#{x}, #{y})" end
def inspect; "#<#{self.class.name} #{to_s}>" end
end
c = Coordinates.new 10, 20
# => #<Coordinates (10, 20)>
The splat operator (*) is a form of explicitconversion to array:
splat 运算符 ( *) 是一种显式转换为数组的形式:
c2 = Coordinates.new *c
# to_a called
# => #<Coordinates (10, 20)>
On the other hand, parallel assignment is a form of implicitconversion to array:
另一方面,并行赋值是一种隐式转换为数组的形式:
x, y = c
# to_ary called
puts x
# 10
puts y
# 20
And so is capturing collection members in block arguments:
在块参数中捕获集合成员也是如此:
[c, c2].each { |(x, y)| puts "Coordinates: #{x}, #{y}" }
# to_ary called
# Coordinates: 10, 20
# to_ary called
# Coordinates: 10, 20
Examples tested on ruby-1.9.3-p0.
上测试的示例ruby-1.9.3-p0。
This pattern seems to be used all over the Ruby language, as evidenced by method pairs like to_sand to_str, to_iand to_intand possibly more.
这种模式似乎是各地使用Ruby语言,如方法对像证明to_s和to_str,to_i并to_int可能更多。
References:
参考:
回答by siame
to_aryallows an object to be treated as an array, whereas to_aactually tries to convert the parameter into an array.
to_ary允许将对象视为数组,而to_a实际上尝试将参数转换为数组。
to_arycan be useful for parallel assignment, whereas to_ais more suited for an actual conversion.
to_ary可用于并行分配,而to_a更适合实际转换。
回答by sgtFloyd
Quoted from gabew's web space:
引用自gabew 的网络空间:
Calling #to_a will convert the receiver to an Array, while #to_ary will not.
调用 #to_a 会将接收器转换为数组,而 #to_ary 则不会。
ruby-1.9.2-p290 :001 > class A < Array; end
ruby-1.9.2-p290 :004 > A[].to_a.class
=> Array
ruby-1.9.2-p290 :005 > A[].to_ary.class
=> A
回答by lfender6445
to_a, when called on an object returns an array representation of obj
to_a,当在对象上调用时返回 obj 的数组表示
Examples
例子
class Example
def initialize
@str = 'example'
end
end
a = Example.new #=> #<Example:0x105a74af8 @str="example"
a.to_a #=> [#<Example:0x105a6a3a0 @str="example">]
Hash Example
哈希示例
h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
h.to_a #=> [["c", 300], ["a", 100], ["d", 400]]
An array can also be created by using the Array() method, provided by Kernel, which tries to call to_ary, then to_a on its argument. http://ruby-doc.org/core-2.0/Array.html#method-i-to_ary
也可以使用内核提供的 Array() 方法创建数组,该方法尝试调用 to_ary,然后调用 to_a 对其参数。http://ruby-doc.org/core-2.0/Array.html#method-i-to_ary
so as far as I can see, the Array#to_ary src just returns the array that is passed in, as in
据我所知, Array#to_ary src 只返回传入的数组,如
def to_ary
return self
end
if I understand correctly, to_a is used for array conversion and makes its final return using to_ary. But this may not be true in future versions according to apidock
如果我理解正确,to_a 用于数组转换并使用 to_ary 使其最终返回。但根据 apidock 的说法,这在未来的版本中可能并非如此
to_a Returns an array representation of obj. For objects of class Object and others that don't explicitly override the method, the return value is an array containing self. However, this latter behavior will soon be obsolete. http://apidock.com/ruby/Object/to_a
to_a 返回 obj 的数组表示。对于 Object 类的对象和其他没有显式覆盖该方法的对象,返回值是一个包含 self 的数组。但是,后一种行为很快就会过时。http://apidock.com/ruby/Object/to_a

