module MoreCoreExtensions::ArrayStretch

Public Instance Methods

stretch(*arys) click to toggle source

Stretch receiver to be the same size as the longest argument Array.

[1, 2].stretch([3, 4], [5, 6, 7])  #=> [1, 2, nil]
# File lib/more_core_extensions/core_ext/array/stretch.rb, line 24
def stretch(*arys)
  self.dup.stretch!(*arys)
end
stretch!(*arys) click to toggle source

Stretch receiver to be the same size as the longest argument Array. Modifies the receiver in place.

[1, 2].stretch!([3, 4], [5, 6, 7])  #=> [1, 2, nil]
# File lib/more_core_extensions/core_ext/array/stretch.rb, line 31
def stretch!(*arys)
  max_size = (arys + [self]).collect { |a| a.length }.max
  self[max_size - 1] = nil unless self.length == max_size
  return self
end
zip_stretched(*arys) click to toggle source

Zip arguments stretching the receiver if necessary. Ruby’s zip method will only zip up to the number of the receiver’s elements if the receiver is shorter than the argument Arrays. This method will zip nils instead of stopping.

[1, 2].zip([3, 4], [5, 6, 7])  #=> [[1, 3, 5], [2, 4, 6]]
[1, 2].zip_stretched([3, 4], [5, 6, 7])  #=> [[1, 3, 5], [2, 4, 6], [nil, nil, 7]
# File lib/more_core_extensions/core_ext/array/stretch.rb, line 44
def zip_stretched(*arys)
  self.stretch(*arys).zip(*arys)
end