Class Array
In: plugins/mcollective/application/inventory.rb
Parent: Object

Monkey patching array with a in_groups_of method that walks an array in groups, pass a block to call the block on each sub array

Methods

Public Instance methods

[Source]

    # File plugins/mcollective/application/inventory.rb, line 7
 7:     def in_groups_of(chunk_size, padded_with=nil)
 8:         arr = self.clone
 9: 
10:         # how many to add
11:         padding = chunk_size - (arr.size % chunk_size)
12: 
13:         # pad at the end
14:         arr.concat([padded_with] * padding)
15: 
16:         # how many chunks we'll make
17:         count = arr.size / chunk_size
18: 
19:         # make that many arrays
20:         result = []
21:         count.times {|s| result <<  arr[s * chunk_size, chunk_size]}
22: 
23:         if block_given?
24:             result.each{|a| yield(a)}
25:         else
26:             result
27:         end
28:     end

[Validate]