diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-11-17 02:48:08 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-11-17 02:48:08 +0000 |
commit | 129b3bdcead4c3cd5999a0069d2e13e4e5a043bf (patch) | |
tree | d0896a706ae4210bda951ed0392b4f2cef2aea24 /activesupport/lib | |
parent | bff217272dbdb17f366f9986496ef04e2599d2c2 (diff) | |
download | rails-129b3bdcead4c3cd5999a0069d2e13e4e5a043bf.tar.gz rails-129b3bdcead4c3cd5999a0069d2e13e4e5a043bf.tar.bz2 rails-129b3bdcead4c3cd5999a0069d2e13e4e5a043bf.zip |
Clarify Array#in_groups_of implementation, don't dup unless needed, only require enumerator once.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8161 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/array/grouping.rb | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/grouping.rb b/activesupport/lib/active_support/core_ext/array/grouping.rb index fae23d4af9..52ed61d3dc 100644 --- a/activesupport/lib/active_support/core_ext/array/grouping.rb +++ b/activesupport/lib/active_support/core_ext/array/grouping.rb @@ -1,3 +1,5 @@ +require 'enumerator' + module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module Array #:nodoc: @@ -21,14 +23,23 @@ module ActiveSupport #:nodoc: # ["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? unless fill_with == false - grouped_collection = [] unless block_given? - collection.each_slice(number) do |group| - block_given? ? yield(group) : grouped_collection << group + if fill_with == false + collection = self + else + # size % number gives how many extra we have; + # subtracting from number gives how many to add; + # modulo number ensures we don't add group of just fill. + padding = (number - size % number) % number + collection = dup.concat([fill_with] * padding) + end + + if block_given? + collection.each_slice(number, &block) + else + returning [] do |groups| + collection.each_slice(number) { |group| groups << group } + end end - grouped_collection unless block_given? end # Divide the array into one or more subarrays based on a delimiting +value+ @@ -40,12 +51,14 @@ module ActiveSupport #:nodoc: # (1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]] def split(value = nil, &block) block ||= Proc.new { |e| e == value } + inject([[]]) do |results, element| if block.call(element) results << [] else results.last << element end + results end end |