a = [ "a", "b", "c", "d" ]
a.rotate #=> ["b", "c", "d", "a"]
#rotate
is a method of Array
in Ruby 1.9. I want this functionality in Ruby 1.8.7. What is the ideal code?
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 []
!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With