Sha256: b29a8b8710a49350f1110ca89ada1b3687cc3bf959e4e8f2ccd177acdefd5f23

Contents?: true

Size: 800 Bytes

Versions: 10

Compression:

Stored size: 800 Bytes

Contents

class Array

  # Rotates an array's elements from back to front n times.
  #
  #   [1,2,3].rotate      #=> [3,1,2]
  #   [3,1,2].rotate      #=> [2,3,1]
  #   [3,1,2].rotate      #=> [1,2,3]
  #   [1,2,3].rotate(3)   #=> [1,2,3]
  #
  # A negative parameter reverses the order from front to back.
  #
  #   [1,2,3].rotate(-1)  #=> [2,3,1]
  #
  # CREDIT: Florian Gross, Thomas Sawyer

  def rotate(n=1)
    self.dup.rotate!(n)
  end

  # Same as #rotate, but acts in place.
  #
  #   a = [1,2,3]
  #   a.rotate!
  #   a  #=> [3,1,2]
  #
  # CREDIT: Florian Gross, Thomas Sawyer

  def rotate!(n=1)
    n = n.to_int
    return self if (n == 0 or self.empty?)
    if n > 0
      n.abs.times{ self.unshift( self.pop ) }
    else
      n.abs.times{ self.push( self.shift ) }
    end
    self
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/array/rotate.rb
facets-2.8.3 lib/core/facets/array/rotate.rb
facets-2.8.2 lib/core/facets/array/rotate.rb
facets-2.8.1 lib/core/facets/array/rotate.rb
facets-2.8.0 lib/core/facets/array/rotate.rb
facets-2.7.0 lib/core/facets/array/rotate.rb
facets-2.6.0 lib/core/facets/array/rotate.rb
facets-2.5.1 lib/core/facets/array/rotate.rb
facets-2.5.0 lib/core/facets/array/rotate.rb
facets-2.5.2 lib/core/facets/array/rotate.rb