【问题标题】:What is the difference between rails in_groups and in_groups_of?rails in_groups 和 in_groups_of 有什么区别?
【发布时间】:2015-08-18 12:45:54
【问题描述】:

这两种方法听起来应该做同样的事情,但它们似乎不是彼此的别名。 in_groupsin_groups_of有什么区别?

【问题讨论】:

  • 一个基本上是切片,另一个将它们分成大小相等的组。明白了。
  • 另一种思考方式:in_groups(n) 返回一个带有n rows 的二维数组,而in_groups_of(n) 返回一个带有n 的二维数组列.

标签: ruby-on-rails ruby


【解决方案1】:

文档很清楚。

in_groups(number, fill_with = nil)

拆分或迭代 number 中的数组,用 fill_with 填充任何剩余的槽,除非它为 false。

in_groups_of(number, fill_with = nil)

大小组 number中拆分或迭代数组,用fill_with填充任何剩余的槽,除非它为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","b","c","d","e","f"].in_groups(2)
# => [["a", "b", "c"], ["d", "e", "f"]]

【讨论】:

    猜你喜欢
    • 2014-11-30
    • 2023-03-26
    • 2017-06-06
    • 2014-07-13
    • 1970-01-01
    • 2011-01-23
    • 2016-03-06
    • 2010-11-17
    • 2023-04-02
    相关资源
    最近更新 更多