a = [ \"a\", \"b\", \"c\", \"d\" ]
a.rotate #=> [\"b\", \"c\", \"d\", \"a\"]
#rotate
is a method of Array
in R
For the rotate! version without the parameter, gnab's is good. If you want the non-destructive one, with an optional parameter:
class Array
def rotate n = 1; self[n..-1]+self[0...n] end
end
If n may become larger than the length of the array:
class Array
def rotate n = 1; return self if empty?; n %= length; self[n..-1]+self[0...n] end
end