Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何迭代连续元素_Arrays_Ruby - Fatal编程技术网

Arrays 如何迭代连续元素

Arrays 如何迭代连续元素,arrays,ruby,Arrays,Ruby,我正在寻找一种类似于数组#组合的方法,但顺序很重要 鉴于这一投入: array = ['a','b','c','d','e'] 我想得到: [['a','b','c'],['b','c','d'],['c','d','e']] 我正试图找到实现这一点的方法: array = ['a','b','c','d','e'] x,a = 3,[] until x > (ary.size) do a += (0.upto(ary.size - x).map{|i| ary[i..(x

我正在寻找一种类似于
数组#组合
的方法,但顺序很重要

鉴于这一投入:

array = ['a','b','c','d','e']
我想得到:

[['a','b','c'],['b','c','d'],['c','d','e']]
我正试图找到实现这一点的方法:

array = ['a','b','c','d','e']
x,a = 3,[] 

until x > (ary.size) do 
  a += (0.upto(ary.size - x).map{|i|  ary[i..(x-1)+i]} )
  x += 1 
end

可枚举文档是您的朋友:

array = ['a','b','c','d','e']
array.each_cons(3).to_a
# => [["a", "b", "c"], ["b", "c", "d"], ["c", "d", "e"]]

为每个连续元素数组迭代给定的块。如果未给定块,则返回枚举数


可枚举文档是您的朋友:

array = ['a','b','c','d','e']
array.each_cons(3).to_a
# => [["a", "b", "c"], ["b", "c", "d"], ["c", "d", "e"]]

为每个连续元素数组迭代给定的块。如果未给定块,则返回枚举数


你看过数组和可枚举文档了吗?我只看了Arrray你看过数组和可枚举文档了吗?我只看了Arrrayah我看到了我只看了Arrayah我看到了我只看了Array