Ruby,在循环数组时跳过项目?

JP *_*shy 1 ruby arrays

说我有一个像这样的数组:

[ 
  {"timestamp"=>"1", "count"=>4488.0},
  {"timestamp"=>"2", "count"=>4622.0},
  {"timestamp"=>"3", "count"=>4655.0},
  {"timestamp"=>"4", "count"=>4533.0},
  {"timestamp"=>"5", "count"=>4439.0},
  {"timestamp"=>"6", "count"=>4468.0},
  {"timestamp"=>"7", "count"=>4419.0},
  {"timestamp"=>"8", "count"=>4430.0},
  {"timestamp"=>"9", "count"=>4429.0},
  {"timestamp"=>"10", "count"=>4502.0},
  {"timestamp"=>"12", "count"=>4497.0},
  {"timestamp"=>"13", "count"=>4468.0},
  {"timestamp"=>"14", "count"=>4510.0},
  {"timestamp"=>"15", "count"=>4547.0},
  {"timestamp"=>"16", "count"=>4471.0},
  {"timestamp"=>"17", "count"=>4501.0},
  {"timestamp"=>"18", "count"=>4451.0},
  {"timestamp"=>"19", "count"=>4453.0},
  {"timestamp"=>"20", "count"=>4593.0},
  {"timestamp"=>"21", "count"=>4540.0},
  {"timestamp"=>"22", "count"=>4516.0},
  {"timestamp"=>"23", "count"=>4494.0}
]
Run Code Online (Sandbox Code Playgroud)

我想循环它将每个x项目放在一个新的数组中,所以就像说我想写一个方法,接受一个类似的my_arr.skip(5)东西?我有点卡在这里,不知道该怎么办.任何帮助都将非常感激.

num*_*407 6

可能的#each_slice可能是你想要的:

# it returns an enumerator, so you could look over it and do whatever
> [1, 2, 3, 4].each_slice(2) {|s| puts s.inspect }
[1, 2]
[3, 4]

# or if you're just looking to group into smaller arrays, you could just...
> [1, 2, 3, 4].each_slice(2).to_a
[[1, 2], [3, 4]]  
Run Code Online (Sandbox Code Playgroud)