They seem to do the same thing.
他们似乎做同样的事情。
g = [{ a: "A" }, { b: "B" }]
r = [{ x: "X" }, { y: "Y" }]
g.zip(r) # => [[{:a=>"A"}, {:x=>"X"}], [{:b=>"B"}, {:y=>"Y"}]]
[g,r].transpose # => [[{:a=>"A"}, {:x=>"X"}], [{:b=>"B"}, {:y=>"Y"}]]
Why have both methods?
为什么有两种方法?
1 个解决方案
#1
12
#transpose
Assumes that self is an array of arrays and transposes the rows and columns.
#transpose假设self是一个数组数组并转换行和列。
#zip
assumes self
can be any Enumerable
object.
#zip假设self可以是任何Enumerable对象。
More differences are here
这里有更多不同之处
a = [12,11,21]
b = [1,2]
[a,b].transpose # transpose': element size differs (2 should be 3) (IndexError)
a.zip(b) # => [[12, 1], [11, 2], [21, nil]]
b.zip(a) # => [[1, 12], [2, 11]]
That to apply the #transpose
method a
and b
should be of the same size. But for applying #zip
, it is not needed b
to be of the same size of a
, ie b
and a
can be of any of size.
应用#transpose方法a和b应该具有相同的大小。但是对于应用#zip,不需要b与a的大小相同,即b和a可以是任何大小。
With #zip
, the resultant array size will always be the size of self
. With #transpose
the resulting array size will be any of the inner array's size of self
.
使用#zip,结果数组大小将始终为self的大小。使用#transpose,生成的数组大小将是内部数组的self大小。
#1
12
#transpose
Assumes that self is an array of arrays and transposes the rows and columns.
#transpose假设self是一个数组数组并转换行和列。
#zip
assumes self
can be any Enumerable
object.
#zip假设self可以是任何Enumerable对象。
More differences are here
这里有更多不同之处
a = [12,11,21]
b = [1,2]
[a,b].transpose # transpose': element size differs (2 should be 3) (IndexError)
a.zip(b) # => [[12, 1], [11, 2], [21, nil]]
b.zip(a) # => [[1, 12], [2, 11]]
That to apply the #transpose
method a
and b
should be of the same size. But for applying #zip
, it is not needed b
to be of the same size of a
, ie b
and a
can be of any of size.
应用#transpose方法a和b应该具有相同的大小。但是对于应用#zip,不需要b与a的大小相同,即b和a可以是任何大小。
With #zip
, the resultant array size will always be the size of self
. With #transpose
the resulting array size will be any of the inner array's size of self
.
使用#zip,结果数组大小将始终为self的大小。使用#transpose,生成的数组大小将是内部数组的self大小。