Skip to content

Commit

Permalink
Performance: grouping helpers should use yield instead of block as ar…
Browse files Browse the repository at this point in the history
…gument. [#723 state:resolved]
  • Loading branch information
miloops authored and jeremy committed Jul 30, 2008
1 parent c403876 commit 2617d0d
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions activesupport/lib/active_support/core_ext/array/grouping.rb
Expand Up @@ -19,7 +19,7 @@ module Grouping
# %w(1 2 3).in_groups_of(2, false) {|g| p g}
# ["1", "2"]
# ["3"]
def in_groups_of(number, fill_with = nil, &block)
def in_groups_of(number, fill_with = nil)
if fill_with == false
collection = self
else
Expand All @@ -31,7 +31,7 @@ def in_groups_of(number, fill_with = nil, &block)
end

if block_given?
collection.each_slice(number, &block)
collection.each_slice(number) { |slice| yield(slice) }
else
returning [] do |groups|
collection.each_slice(number) { |group| groups << group }
Expand Down Expand Up @@ -87,11 +87,11 @@ def in_groups(number, fill_with = nil)
#
# [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]]
# (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 }
def split(value = nil)
using_block = block_given?

inject([[]]) do |results, element|
if block.call(element)
if (using_block && yield(element)) || (value == element)
results << []
else
results.last << element
Expand Down

0 comments on commit 2617d0d

Please sign in to comment.