我想以下列方式拆分我的数组:
current_arr = [1,2,3,4,5]
new_arr = [[1,2,3], [2,3,4], [3,4,5]]
Run Code Online (Sandbox Code Playgroud)
#each_slice并且#combination接近我想要的但不完全.
我怎么能像示例中那样拆分我的数组?
[1,2,3,4,5].each_cons(3).to_a
#=> [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
Run Code Online (Sandbox Code Playgroud)
检查each_cons的 doc .