Array#rotate equivalent in ruby 1.8.7

前端 未结 4 916
南笙
南笙 2021-01-13 00:55
a = [ \"a\", \"b\", \"c\", \"d\" ]
a.rotate         #=> [\"b\", \"c\", \"d\", \"a\"]

#rotate is a method of Array in R

相关标签:
4条回答
  • 2021-01-13 01:22

    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
    
    0 讨论(0)
  • 2021-01-13 01:29

    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 []!

    0 讨论(0)
  • 2021-01-13 01:32

    You can achieve the same with a.push(a.shift). It basically removes the first element (shift) and appends it to the end (push).

    0 讨论(0)
  • 2021-01-13 01:32

    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
    
    0 讨论(0)
提交回复
热议问题