From 9db407f5786d96373d1534faa79c3220685b8ae0 Mon Sep 17 00:00:00 2001 From: Marcel Molina Date: Sat, 2 Sep 2006 18:02:36 +0000 Subject: Don't pad remaining places with in_groups_of if specified padding value is false. [Marcel Molina Jr.] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4900 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/lib/active_support/core_ext/array/grouping.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/array/grouping.rb') diff --git a/activesupport/lib/active_support/core_ext/array/grouping.rb b/activesupport/lib/active_support/core_ext/array/grouping.rb index 6f28e1eb6d..fae23d4af9 100644 --- a/activesupport/lib/active_support/core_ext/array/grouping.rb +++ b/activesupport/lib/active_support/core_ext/array/grouping.rb @@ -3,7 +3,8 @@ module ActiveSupport #:nodoc: module Array #:nodoc: module Grouping # Iterate over an array in groups of a certain size, padding any remaining - # slots with specified value (nil by default). + # slots with specified value (nil by default) unless it is + # false. # # E.g. # @@ -11,10 +12,18 @@ module ActiveSupport #:nodoc: # ["1", "2", "3"] # ["4", "5", "6"] # ["7", nil, nil] + # + # %w(1 2 3).in_groups_of(2, ' ') {|g| p g} + # ["1", "2"] + # ["3", " "] + # + # %w(1 2 3).in_groups_of(2, false) {|g| p g} + # ["1", "2"] + # ["3"] def in_groups_of(number, fill_with = nil, &block) require 'enumerator' collection = dup - collection << fill_with until collection.size.modulo(number).zero? + collection << fill_with until collection.size.modulo(number).zero? unless fill_with == false grouped_collection = [] unless block_given? collection.each_slice(number) do |group| block_given? ? yield(group) : grouped_collection << group -- cgit v1.2.3