Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有一个数组
a = [1, 2, 3, 4, 5, 6]
我想将它重塑为
a = [[1, 2], [3, 4], [5, 6]]
我的印象是有一种特定的方法。我刚刚通过 Array 类参考,但未能找到它。有人记得吗?
你可以这样做:
a = [1, 2, 3, 4, 5, 6] a.each_slice(2).to_a # => [[1, 2], [3, 4], [5, 6]]
像这样,例如: