Add Enumerable#group_by and Array#in_groups_of

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3726 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Marcel Molina
2006-03-01 20:32:10 +00:00
parent 8368b5960b
commit 1fdf578c17
5 changed files with 116 additions and 2 deletions

View File

@@ -1,5 +1,30 @@
*SVN*
* Add Enumerable#group_by for grouping collections based on the result of some
block. Useful, for example, for grouping records by date.
ex.
latest_transcripts.group_by(&:day).each do |day, transcripts|
p "#{day} -> #{transcripts.map(&:class) * ', '}"
end
"2006-03-01 -> Transcript"
"2006-02-28 -> Transcript"
"2006-02-27 -> Transcript, Transcript"
"2006-02-26 -> Transcript, Transcript"
Add Array#in_groups_of, for iterating over an array in groups of a certain
size.
ex.
%w(1 2 3 4 5 6 7).in_groups_of(3) {|g| p g}
["1", "2", "3"]
["4", "5", "6"]
["7", nil, nil]
[Marcel Molina Jr., Sam Stephenson]
* Added Kernel#daemonize to turn the current process into a daemon that can be killed with a TERM signal [DHH]
* Add 'around' methods to Logger, to make it easy to log before and after messages for a given block as requested in #3809. [Michael Koziarski] Example:

View File

@@ -2,4 +2,20 @@ require File.dirname(__FILE__) + '/array/conversions'
class Array #:nodoc:
include ActiveSupport::CoreExtensions::Array::Conversions
# Iterate over an array in groups of a certain size, padding any remaining
# slots with specified value (<tt>nil</tt> by default).
#
# E.g.
#
# %w(1 2 3 4 5 6 7).in_groups_of(3) {|g| p g}
# ["1", "2", "3"]
# ["4", "5", "6"]
# ["7", nil, nil]
def in_groups_of(number, fill_with = nil, &block)
require 'enumerator'
collection = dup
collection << fill_with until collection.size.modulo(number).zero?
collection.each_slice(number, &block)
end
end

View File

@@ -6,4 +6,31 @@ module Enumerable #:nodoc:
end
match
end
end
# Collect an enumerable into sets, grouped by the result of a block. Useful,
# for example, for grouping records by date.
#
# e.g.
#
# latest_transcripts.group_by(&:day).each do |day, transcripts|
# p "#{day} -> #{transcripts.map(&:class) * ', '}"
# end
# "2006-03-01 -> Transcript"
# "2006-02-28 -> Transcript"
# "2006-02-27 -> Transcript, Transcript"
# "2006-02-26 -> Transcript, Transcript"
# "2006-02-25 -> Transcript"
# "2006-02-24 -> Transcript, Transcript"
# "2006-02-23 -> Transcript"
def group_by
inject([]) do |groups, element|
value = yield(element)
if (last_group = groups.last) && last_group.first == value
last_group.last << element
else
groups << [value, [element]]
end
groups
end
end
end

View File

@@ -34,3 +34,34 @@ class ArrayExtConversionTests < Test::Unit::TestCase
assert_equal "one", ['one'].to_sentence
end
end
class ArrayExtGroupingTests < Test::Unit::TestCase
def test_group_by_with_perfect_fit
groups = []
('a'..'i').to_a.in_groups_of(3) do |group|
groups << group
end
assert_equal [%w(a b c), %w(d e f), %w(g h i)], groups
end
def test_group_by_with_padding
groups = []
('a'..'g').to_a.in_groups_of(3) do |group|
groups << group
end
assert_equal [%w(a b c), %w(d e f), ['g', nil, nil]], groups
end
def test_group_by_pads_with_specified_values
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
end
end

View File

@@ -12,4 +12,19 @@ class EnumerableTests < Test::Unit::TestCase
assert_equal true, (1..10).first_match {|x| x > 9}
assert_equal :aba, {:a => 10, :aba => 50, :bac => 40}.first_match {|k, v| k if v > 45}
end
end
def test_group_by
names = %w(marcel sam david jeremy)
klass = Class.new
klass.send(:attr_accessor, :name)
objects = (1..50).inject([]) do |people,|
p = klass.new
p.name = names.sort_by { rand }.first
people << p
end
objects.group_by {|object| object.name}.each do |name, group|
assert group.all? {|person| person.name == name}
end
end
end