3 module ActiveSupport
#:nodoc:
4 module CoreExtensions
#:nodoc:
7 # Splits or iterates over the array in groups of size +number+,
8 # padding any remaining slots with +fill_with+ unless it is +false+.
10 # %w(1 2 3 4 5 6 7).in_groups_of(3) {|group| p group}
15 # %w(1 2 3).in_groups_of(2, ' ') {|group| p group}
19 # %w(1 2 3).in_groups_of(2, false) {|group| p group}
22 def in_groups_of(number
, fill_with
= nil)
26 # size % number gives how many extra we have;
27 # subtracting from number gives how many to add;
28 # modulo number ensures we don't add group of just fill.
29 padding
= (number
- size
% number
) % number
30 collection
= dup
.concat([fill_with
] * padding
)
34 collection
.each_slice(number
) { |slice
| yield(slice
) }
36 returning
[] do |groups
|
37 collection
.each_slice(number
) { |group
| groups
<< group
}
42 # Splits or iterates over the array in +number+ of groups, padding any
43 # remaining slots with +fill_with+ unless it is +false+.
45 # %w(1 2 3 4 5 6 7 8 9 10).in_groups(3) {|group| p group}
46 # ["1", "2", "3", "4"]
47 # ["5", "6", "7", nil]
48 # ["8", "9", "10", nil]
50 # %w(1 2 3 4 5 6 7).in_groups(3, ' ') {|group| p group}
52 # ["4", "5", " "]
53 # ["6", "7", " "]
55 # %w(1 2 3 4 5 6 7).in_groups(3, false) {|group| p group}
59 def in_groups(number
, fill_with
= nil)
60 # size / number gives minor group size;
61 # size % number gives how many objects need extra accomodation;
62 # each group hold either division or division + 1 items.
63 division
= size
/ number
64 modulo
= size
% number
66 # create a new array avoiding dup
70 number
.times
do |index
|
71 length
= division
+ (modulo
> 0 && modulo
> index
? 1 : 0)
72 padding
= fill_with
!= false &&
73 modulo
> 0 && length
== division
? 1 : 0
74 groups
<< slice(start
, length
).concat([fill_with
] * padding
)
79 groups
.each
{|g
| yield(g
) }
85 # Divides the array into one or more subarrays based on a delimiting +value+
86 # or the result of an optional block.
88 # [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]]
89 # (1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]]
90 def split(value
= nil)
91 using_block
= block_given
?
93 inject([[]]) do |results
, element
|
94 if (using_block
&& yield(element
)) || (value
== element
)
97 results
.last
<< element