Array#rotate equivalent in ruby 1.8.7
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开发者_JAVA百科 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 []
!
You can achieve the same with a.push(a.shift)
. It basically removes the first element (shift) and appends it to the end (push).
Nothing like coming way late to the party... ;)
Similar to a.rotate!(n)
:
a += a.shift(n)
And it works with a = []
. However, unlike a.rotate!(n)
, it doesn't do anything if n
is greater than the length of a
.
The following preserves the value of a
and allow n
greater than a.length
to work, at the expense of being a little more elaborate:
a.last(a.length - (n % a.length)) + a.first(n % a.length)
This would obviously be best if n % a.length
is computed once separately and the whole thing wrapped in a method monkey patched into Array
.
class Array
def rot(n)
m = n % self.length
self.last(self.length - m) + self.first(m)
end
end
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
精彩评论