a = [ \"a\", \"b\", \"c\", \"d\" ]
a.rotate #=> [\"b\", \"c\", \"d\", \"a\"]
#rotate
is a method of Array
in R
If you require 'backports/1.9.2/array/rotate', you will get Array#rotate
and rotate!
in older versions of Ruby.
Either way, you avoid reinventing the wheel, and more importantly you gain the advantage of an implementation that passes RubySpec. It will work for all corner cases and ensure compatibility with Ruby 1.9.
For example, none of the two answers given work for []
!