【发布时间】:2015-02-02 02:05:09
【问题描述】:
假设你有这个数组:
arr = w|one two three|
如何通过将两个连续元素作为块参数来迭代它,如下所示:
1st cycle: |nil, 'one'|
2nd cycle: |'one', 'two'|
3rd cycle: |'two', 'three'|
到目前为止,我只带着这个:
arr.each_index { |i| [i - 1 < 0 ? nil: arr[i - 1], arr[i]] }
有更好的解决方案吗?有each(n)之类的吗?
【问题讨论】: