Splitting Hairs and Arrays
Am I just dumb, or is it really a lot harder than it should be to break an array up into a set number of chunks?
For example, I have a list of 8 items that I want to break into 3 arrays, each displayed in their own unordered list, like this:
Brian and I spent a ridiculous amount of time (20 minutes, at least) trying to come up with a clean solution to this seemingly simple problem. The closest thing there is to a solution is Enumerable#each_slice in Ruby core or Array#in_groups_of in Active Support.
or
There’s not really a difference between either solution. Both requires that we calculate how many items we want in each list. (We convert the size to a float, divide by the number of columns, then round up. This gives us the same number of items in each column, with the last column having fewer.)
Our solution
We didn’t like having that much logic in the view, so we added a method to enumerable; we thought the division (/
) method seemed appropriate since we’re dividing the array into equal parts.
Note: this method is now in our awesomeness plugin.
So now we can just divide our array into chunks in the view.
Are we dumb? Is there already a way to do this that wasn’t obvious to us and we just wasted our time (and I wasted even more time blogging about it)?
Update: Thanks to Aaron Pfeifer for pointing out the [discussion on Jay Field's blog](http://blog.jayfields.com/2007/09/ruby-arraychunk.html) about something similar. I've refactored this code in [awesomeness](http://github.com/collectiveidea/awesomeness/tree/master/lib/awesomeness/core_ext/enumerable.rb) to be more "robust' (read: convoluted).