diff options
author | Marcel Molina <marcel@vernix.org> | 2006-09-02 18:02:36 +0000 |
---|---|---|
committer | Marcel Molina <marcel@vernix.org> | 2006-09-02 18:02:36 +0000 |
commit | 9db407f5786d96373d1534faa79c3220685b8ae0 (patch) | |
tree | 30e9d723a1561efad51d8882f3850a14ce9f388c /activesupport | |
parent | 03de0cfe7ab281eda70f2cedd09659752e765acf (diff) | |
download | rails-9db407f5786d96373d1534faa79c3220685b8ae0.tar.gz rails-9db407f5786d96373d1534faa79c3220685b8ae0.tar.bz2 rails-9db407f5786d96373d1534faa79c3220685b8ae0.zip |
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
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/array/grouping.rb | 13 | ||||
-rw-r--r-- | activesupport/test/core_ext/array_ext_test.rb | 12 |
3 files changed, 24 insertions, 3 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index cf66ee470e..98342358ab 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Don't pad remaining places with in_groups_of if specified padding value is false. [Marcel Molina Jr.] + * Fix cases where empty xml nodes weren't being translated to nil in Hash.create_from_xml [Rick Olson] <written-on type="date"></written-on> # => { :type => 'date' } # WRONG 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 (<tt>nil</tt> by default). + # slots with specified value (<tt>nil</tt> by default) unless it is + # <tt>false</tt>. # # 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 diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 955b2b30c1..64c9a8b4ff 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -74,11 +74,21 @@ class ArrayExtGroupingTests < Test::Unit::TestCase def test_group_by_pads_with_specified_values groups = [] + ('a'..'g').to_a.in_groups_of(3, 'foo') do |group| + groups << group + end + + assert_equal [%w(a b c), %w(d e f), ['g', 'foo', 'foo']], groups + end + + def test_group_without_padding + groups = [] + ('a'..'g').to_a.in_groups_of(3, false) do |group| groups << group end - assert_equal [%w(a b c), %w(d e f), ['g', false, false]], groups + assert_equal [%w(a b c), %w(d e f), ['g']], groups end end |