ruby-on-rails – rails in_groups和in_groups_of之间有什么区别?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – rails in_groups和in_groups_of之间有什么区别?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这两种方法都听起来像他们应该做同样的事情,但是它们似乎不是彼此的别名. in_groups和in_groups_of有什么区别?

> Array#in_groups
> Array#in_groups_of

解决方法

文件很清楚.

in_groups(number,fill_with = nil)

Splits or iterates over the array in number of groups,padding any remaining slots with fill_with unless it is false.

in_groups_of(number,fill_with = nil)

Splits or iterates over the array in groups of size number,padding any remaining slots with fill_with unless it is false.

例:

# Splits in groups of 2
["a","b","c","d","e","f"].in_groups_of(2)
# => [["a","b"],["c","d"],["e","f"]]

# Splits in 2 groups
["a","f"].in_groups(2)
# => [["a","c"],["d","f"]]

猜你在找的Ruby相关文章