File: rotate.rb

package info (click to toggle)
ruby-backports 3.25.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,912 kB
  • sloc: ruby: 11,759; makefile: 6
file content (17 lines) | stat: -rw-r--r-- 338 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
unless Array.method_defined? :rotate
  class Array
    def rotate(n=1)
      Array.new(self).rotate!(n)
    end
  end
end

unless Array.method_defined? :rotate!
  require 'backports/tools/arguments'
  class Array
    def rotate!(n=1)
      n = Backports.coerce_to_int(n) % (empty? ? 1 : size)
      concat(slice!(0, n))
    end
  end
end