Ruby 从一维数组怎样构建基于他取模的二位数组

flowerwrong · January 16, 2015 · Last by Kabie replied at January 16, 2015 · 1963 hits

比如说

arr = [1,2,3,4,5,6,7,8,9,10]

# 得到如下数组
arr_per_four = [
  [1,2,3,4],
  [5,6,7,8],
  [9,10]
]

# 或者如下
arr_per_three = [
  [1,2,3],
  [4,5,6],
  [7,8,9],
  [10]
]

请问怎么最简单?

arr.each_slice(n).to_a

You need to Sign in before reply, if you don't have an account, please Sign up first.