This project is archived and is in readonly mode.
Add in_groups to ActiveSupport::CoreExtensions::Array::Grouping
-
Pratik
- State changed from new to wontfix
I feel it's bit too complex. And extensions like this are a better fit for Rails.root/lib rather than in core.
But if you disagree, please continue the discussion in rails core mailing list.
Thanks.
-
Adrian Mugnolo
Pratik,
I would gladly continue the discussion in the Rails Core mailing list. Still, let me comment on complexity and use cases.
The method interface matches 1:1 the one for in_groups_of -- which in the scope of Rails, lays on the easy side. So use shouldn't be a problem to anyone already using in_groups_of.
Both methods appear as a natural fit. When slicing an array you're usually concerned about either: subarray size (where you would use in_groups_of) or, number of subarrays (where you could use in_groups).
Possible use cases are: view code for n-column layout as in "x accounts/addresses to be splitted in y elements to be floated and rendered as columns"; resource allocation as in "x conference attendees to be arranged along y available rooms"; and many others.
-
Adrian Mugnolo
Fixed a bug in test_in_groups_returned_array_size.
-
RSL
I found the example code in the ticket a little confusing and didn't see the point. Then it clicked in my head that where the existing method says "divide this into n groups of three" this says "divide this into three groups of n". You can see this a little better in:
- Produces four groups of three
>> %w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3)
=> [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"], ["10", nil, nil]]
- Produces three groups of four
%w(1 2 3 4 5 6 7 8 9 10).in_groups(3)
=> [["1", "2", "3", "4"], ["5", "6", "7", nil], ["8", "9", "10", nil]]
It seems like a natural converse method to me. :)
-
RSL
Those numbers were supposed to be comments using #. Silly me forgot about formatting.
-
Pratik
- State changed from wontfix to open
-
Adrian Mugnolo
RSL, thanks for coming up with much better wording and code samples! :-)
-
Tarmo Tänav
Looks useful, +1
Though I'd like to point out that if anyone should need a columns-first order (which gives the same fill as in_groups, but the elements are first ordered by row and then column), there are two ways:
- Here 4 is the number of result groups
>> %w(1 2 3 4 5 6 7 8 9 10).in_groups_of(4).transpose
=> [["1", "5", "9"], ["2", "6", "10"], ["3", "7", nil], ["4", "8", nil]]
- Or another way to do the same:
>> y=%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(4)
=> [["1", "2", "3", "4"], ["5", "6", "7", "8"], ["9", "10", nil, nil]]
>> y.shift.zip(*y)
=> [["1", "5", "9"], ["2", "6", "10"], ["3", "7", nil], ["4", "8", nil]]
-
Adrian Mugnolo
Summarized both commits as one.
-
Repository
- State changed from open to resolved
(from [fc89a951933638b051bb1f9e1339ee6ae7c94cda]) Add in_groups to ActiveSupport::CoreExtensions::Array::Grouping. [#579 state:resolved]
Signed-off-by: Pratik Naik
