Sha256: 6bec2cfe0141ce14276faf8c0fec0b56f309e48c8092832aaa83646c33e6b11b

Contents?: true

Size: 825 Bytes

Versions: 8

Compression:

Stored size: 825 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
  #   CREDIT 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
  #   CREDIT 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

8 entries across 8 versions & 1 rubygems

Version Path
facets-2.0.2 lib/core/facets/array/rotate.rb
facets-2.0.4 lib/core/facets/array/rotate.rb
facets-2.1.2 lib/core/facets/array/rotate.rb
facets-2.0.5 lib/core/facets/array/rotate.rb
facets-2.0.3 lib/core/facets/array/rotate.rb
facets-2.1.1 lib/core/facets/array/rotate.rb
facets-2.1.0 lib/core/facets/array/rotate.rb
facets-2.1.3 lib/core/facets/array/rotate.rb