Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Array#rotate!
array.rotate! → self
array.rotate!(count) → self
array.c
View on GitHub ↗
static VALUE
rb_ary_rotate_bang(int argc, VALUE *argv, VALUE ary)
{
long n = (rb_check_arity(argc, 0, 1) ? NUM2LONG(argv[0]) : 1);
rb_ary_rotate(ary, n);
return ary;
}
Rotates self in place by moving elements from one end to the other; returns self.
When no argument given, rotates the first element to the last position:
Example
a = [:foo, 'bar', 2, 'bar'] a.rotate! # => ["bar", 2, "bar", :foo]
When given a non-negative Integer count, rotates count elements from the beginning to the end:
Example
a = [:foo, 'bar', 2] a.rotate!(2) a # => [2, :foo, "bar"]
If count is large, uses count % array.size as the count:
Example
a = [:foo, 'bar', 2] a.rotate!(20) a # => [2, :foo, "bar"]
If count is zero, returns self unmodified:
Example
a = [:foo, 'bar', 2] a.rotate!(0) a # => [:foo, "bar", 2]
When given a negative Integer count, rotates in the opposite direction, from end to beginning:
Example
a = [:foo, 'bar', 2] a.rotate!(-2) a # => ["bar", 2, :foo]
If count is small (far from zero), uses count % array.size as the count:
Example
a = [:foo, 'bar', 2] a.rotate!(-5) a # => ["bar", 2, :foo]