红宝石阵列 - 反向迭代和下一元素之前 [英] Ruby Array - reverse iterate with next and before element
本文介绍了红宝石阵列 - 反向迭代和下一元素之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何扭转遍历数组旁边和当前元素的元素前?
How to reverse iterate an Array with next and before element of the current element ?
时有可能使用 each_cons
与 reverse_each
?
推荐答案
是的,这是可能的。
[1,2,3,4,5,6].reverse_each.each_cons(3) { |before, current, next_|
p [before, current, next_]
}
打印
[6, 5, 4]
[5, 4, 3]
[4, 3, 2]
[3, 2, 1]
([nil]+[1,2,3,4,5,6]+[nil]).reverse_each.each_cons(3) { |before, current, next_|
p [before, current, next_]
}
打印
[nil, 6, 5]
[6, 5, 4]
[5, 4, 3]
[4, 3, 2]
[3, 2, 1]
[2, 1, nil]
这篇关于红宝石阵列 - 反向迭代和下一元素之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文