Array#rotate equivalent in ruby 1.8.7

前端 未结 4 921
南笙
南笙 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: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 []!

提交回复
热议问题