Sha256: 00af9830fea21597aff37a6e26fa3a0042f9ae00f6e908baf32f6b634222c676

Contents?: true

Size: 874 Bytes

Versions: 15

Compression:

Stored size: 874 Bytes

Contents

class Array

  # Pad an array with a given <tt>value</tt> up to 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

15 entries across 14 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/array/pad.rb
facets-3.1.0 lib/core/facets/array/pad.rb
facets-3.0.0 lib/core/facets/array/pad.rb
facets-2.9.3 lib/core/facets/array/pad.rb
facets-2.9.2 src/core/facets/array/pad.rb
facets-2.9.2 lib/core/facets/array/pad.rb
facets-2.9.1 lib/core/facets/array/pad.rb
facets-2.9.0 lib/core/facets/array/pad.rb
facets-2.9.0.pre.2 lib/core/facets/array/pad.rb
facets-2.9.0.pre.1 lib/core/facets/array/pad.rb
facets-2.8.4 lib/core/facets/array/pad.rb
facets-2.8.3 lib/core/facets/array/pad.rb
facets-2.8.2 lib/core/facets/array/pad.rb
facets-2.8.1 lib/core/facets/array/pad.rb
facets-2.8.0 lib/core/facets/array/pad.rb