Skip to content
This repository
Browse code

Don't pad remaining places with in_groups_of if specified padding val…

…ue is false. [Marcel Molina Jr.]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4900 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 9db407f5786d96373d1534faa79c3220685b8ae0 1 parent 03de0cf
authored September 02, 2006
2  activesupport/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Don't pad remaining places with in_groups_of if specified padding value is false. [Marcel Molina Jr.]
  4
+
3 5
 * Fix cases where empty xml nodes weren't being translated to nil in Hash.create_from_xml [Rick Olson]
4 6
 
5 7
   <written-on type="date"></written-on> # => { :type => 'date' } # WRONG
13  activesupport/lib/active_support/core_ext/array/grouping.rb
@@ -3,7 +3,8 @@ module CoreExtensions #:nodoc:
3 3
     module Array #:nodoc:
4 4
       module Grouping
5 5
         # Iterate over an array in groups of a certain size, padding any remaining 
6  
-        # slots with specified value (<tt>nil</tt> by default).
  6
+        # slots with specified value (<tt>nil</tt> by default) unless it is
  7
+        # <tt>false</tt>.
7 8
         # 
8 9
         # E.g.
9 10
         # 
@@ -11,10 +12,18 @@ module Grouping
11 12
         #   ["1", "2", "3"]
12 13
         #   ["4", "5", "6"]
13 14
         #   ["7", nil, nil]
  15
+        #
  16
+        #   %w(1 2 3).in_groups_of(2, '&nbsp;') {|g| p g}
  17
+        #   ["1", "2"]
  18
+        #   ["3", "&nbsp;"]
  19
+        #
  20
+        #   %w(1 2 3).in_groups_of(2, false) {|g| p g}
  21
+        #   ["1", "2"]
  22
+        #   ["3"]
14 23
         def in_groups_of(number, fill_with = nil, &block)
15 24
           require 'enumerator'
16 25
           collection = dup
17  
-          collection << fill_with until collection.size.modulo(number).zero?
  26
+          collection << fill_with until collection.size.modulo(number).zero? unless fill_with == false
18 27
           grouped_collection = [] unless block_given?
19 28
           collection.each_slice(number) do |group|
20 29
             block_given? ? yield(group) : grouped_collection << group
12  activesupport/test/core_ext/array_ext_test.rb
@@ -74,11 +74,21 @@ def test_group_by_with_padding
74 74
   def test_group_by_pads_with_specified_values
75 75
     groups = []
76 76
 
  77
+    ('a'..'g').to_a.in_groups_of(3, 'foo') do |group|
  78
+      groups << group
  79
+    end
  80
+
  81
+    assert_equal [%w(a b c), %w(d e f), ['g', 'foo', 'foo']], groups
  82
+  end
  83
+
  84
+  def test_group_without_padding
  85
+    groups = []
  86
+
77 87
     ('a'..'g').to_a.in_groups_of(3, false) do |group|
78 88
       groups << group
79 89
     end
80 90
 
81  
-    assert_equal [%w(a b c), %w(d e f), ['g', false, false]], groups
  91
+    assert_equal [%w(a b c), %w(d e f), ['g']], groups
82 92
   end
83 93
 end
84 94
 

0 notes on commit 9db407f

Please sign in to comment.
Something went wrong with that request. Please try again.