Module: MoreCoreExtensions::ArrayStretch::ClassMethods

Defined in:
lib/more_core_extensions/core_ext/array/stretch.rb

Instance Method Summary collapse

Instance Method Details

#stretch(*arys) ⇒ Object

Stretch all argument Arrays to make them the same size.

Array.stretch([1, 2], [3, 4], [5, 6, 7])  #=> [[1, 2, nil], [3, 4, nil], [5, 6, 7]]


7
8
9
# File 'lib/more_core_extensions/core_ext/array/stretch.rb', line 7

def stretch(*arys)
  self.stretch!(*arys.collect { |a| a.dup })
end

#stretch!(*arys) ⇒ Object

Stretch all argument Arrays to make them the same size. Modifies the arguments in place.

Array.stretch!([1, 2], [3, 4], [5, 6, 7])  #=> [[1, 2, nil], [3, 4, nil], [5, 6, 7]]


14
15
16
17
18
# File 'lib/more_core_extensions/core_ext/array/stretch.rb', line 14

def stretch!(*arys)
  max_size = arys.collect { |a| a.length }.max
  arys.each { |a| a[max_size - 1] = nil unless a.length == max_size }
  return *arys
end