public
Description: Collective Idea's Awesomeness. A collection of useful Rails bits and pieces.
Homepage:
Clone URL: git://github.com/collectiveidea/awesomeness.git
Click here to lend your support to: awesomeness and make a donation at www.pledgie.com !
awesomeness / lib / awesomeness / core_ext / enumerable.rb
100644 14 lines (12 sloc) 0.415 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
module Enumerable
 
  # Divide into groups
  def chunk(number_of_chunks, padding = false)
    chunk_size = (size.to_f / number_of_chunks).ceil
    chunk_size = 1 if chunk_size < 1
    returning enum_for(:each_slice, chunk_size).to_a do |result|
      result << [] while result.length < number_of_chunks
      result.last.pad!(result.first.length, padding) unless padding == false
    end
  end
  alias / chunk
  
end