您的位置:首页 > 其它

divide an array by the size

2014-07-13 19:00 330 查看
For example,i have an array :[2,4,6,7,9,12,1],i want to divide it by the size [2,2,3]

the output that i want is:
[[2,4],[6,7],[9,12,1]]


The way looks correct, you can do it more succinctly using 
map
 instead
of 
each
:

c = b.map{|m| a.shift(m)}

Or, using 
&method
 shorthand:

c = b.map(&a.method(:shift))
# => [[2, 4], [6, 7], [9, 12, 1]]


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐