Skip to content
instance method

Array#rotate!

(?::int count) -> self
rotate!(count = 1) → self

Rotates self in place by moving elements from one end to the other; returns self.

With non-negative numeric count, rotates count elements from the beginning to the end:

Example
[0, 1, 2, 3].rotate!(2)   # => [2, 3, 0, 1]
[0, 1, 2, 3].rotate!(2.1) # => [2, 3, 0, 1]

If count is large, uses count % array.size as the count:

Example
[0, 1, 2, 3].rotate!(21) # => [1, 2, 3, 0]

If count is zero, rotates no elements:

Example
[0, 1, 2, 3].rotate!(0) # => [0, 1, 2, 3]

With a negative numeric count, rotates in the opposite direction, from end to beginning:

Example
[0, 1, 2, 3].rotate!(-1) # => [3, 0, 1, 2]

If count is small (far from zero), uses count % array.size as the count:

Example
[0, 1, 2, 3].rotate!(-21) # => [3, 0, 1, 2]

Related: see Methods for Assigning.

See also #rotate #sample #rindex #select also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0