Sha256: e3579ad3fd61f015b144e92835693a47f119226b5500c4cf1f71d8aa305909f6

Contents?: true

Size: 875 Bytes

Versions: 7

Compression:

Stored size: 875 Bytes

Contents

class Array

  # Pad an array with a given <tt>value</tt> upto a given <tt>length</tt>.
  #
  #   [0,1,2].pad(6,"a")  #=> [0,1,2,"a","a","a"]
  #
  # If <tt>length</tt> is a negative number padding will be added
  # to the beginning of the array.
  #
  #   [0,1,2].pad(-6,"a")  #=> ["a","a","a",0,1,2]
  #
  #  CREDIT: Richard Laugesen

  def pad(len, val=nil)
    return dup if self.size >= len.abs
    if len < 0
      Array.new((len+size).abs,val) + self
    else
      self + Array.new(len-size,val)
    end
  end

  # Like #pad but changes the array in place.
  #
  #    a = [0,1,2]
  #    a.pad!(6,"x")
  #    a  #=> [0,1,2,"x","x","x"]
  #
  #  CREDIT: Richard Laugesen

  def pad!(len, val=nil)
    return self if self.size >= len.abs
    if len < 0
      replace Array.new((len+size).abs,val) + self
    else
      concat Array.new(len-size,val)
    end
  end

end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
facets-2.4.0 lib/facets/array/pad.rb
facets-2.4.1 lib/facets/array/pad.rb
facets-2.4.2 lib/core/facets/array/pad.rb
facets-2.4.3 lib/core/facets/array/pad.rb
facets-2.4.4 lib/core/facets/array/pad.rb
facets-2.4.5 lib/core/facets/array/pad.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/array/pad.rb