From d6424f05c3781c4f14e00ff3faa1da1d1c244775 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 2 Jun 2009 00:13:00 +0200 Subject: AS guide: explains array grouping --- .../guides/source/active_support_overview.textile | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index 3bdaa022e0..22539112e0 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/railties/guides/source/active_support_overview.textile @@ -245,6 +245,98 @@ You can pick a random element with +rand+: shape_type = [Circle, Square, Triangle].rand +h4. Grouping + +h5. +in_groups_of(number, fill_with = nil)+ + +The method +in_groups_of+ splits an array into consecutive groups of a certain size. It returns an array with the groups: + + +[1, 2, 3].in_groups_of(2) # => [[1, 2], [3, nil]] + + +or yields them in turn if a block is passed: + + +<% sample.in_groups_of(3) do |a, b, c| %> + + <%=h a %> + <%=h b %> + <%=h c %> + +<% end %> + + +The first example shows +in_groups_of+ fills the last group with as many +nil+ elements as needed to have the requested size. You can change this padding value using the second optional argument: + + +[1, 2, 3].in_groups_of(2, 0) # => [[1, 2], [3, 0]] + + +And you can tell the method not to fill the last group passing +false+: + + +[1, 2, 3].in_groups_of(2, false) # => [[1, 2], [3]] + + +As a consequence +false+ can't be a used as a padding value. + +h5. +in_groups(number, fill_with = nil)+ + +The method +in_groups+ splits an array into a certain number of groups. The method returns and array with the groups: + + +%w(1 2 3 4 5 6 7).in_groups(3) +# => [["1", "2", "3"], ["4", "5", nil], ["6", "7", nil]] + + +or yields them in turn if a block is passed: + + +%w(1 2 3 4 5 6 7).in_groups(3) {|group| p group} +["1", "2", "3"] +["4", "5", nil] +["6", "7", nil] + + +The examples above show that +in_groups+ fills some groups with a trailing +nil+ element as needed. A group can get at most one of these extra elements, the rightmost one if any. And the groups that have them are always the last ones. + +You can change this padding value using the second optional argument: + + +%w(1 2 3 4 5 6 7).in_groups(3, "0") +# => [["1", "2", "3"], ["4", "5", "0"], ["6", "7", "0"]] + + +And you can tell the method not to fill the smaller groups passing +false+: + + +%w(1 2 3 4 5 6 7).in_groups(3, false) +# => [["1", "2", "3"], ["4", "5"], ["6", "7"]] + + +As a consequence +false+ can't be a used as a padding value. + +h5. +split(value = nil)+ + +The method +split+ divides an array by a separator and returns the resulting chunks. + +If a block is passed the separators are those elements of the array for which the block returns true: + + +(-5..5).to_a.split { |i| i.multiple_of?(4) } +# => [[-5], [-3, -2, -1], [1, 2, 3], [5]] + + +Otherwise, the value received as argument, which defaults to +nil+, is the separator: + + +[0, 1, -5, 1, 1, "foo", "bar"].split(1) +# => [[0], [-5], [], ["foo", "bar"]] + + +NOTE: Observe in the previous example that consecutive separators result in empty arrays. + h3. Extensions to +Hash+ ... -- cgit v1.2.3