From 0898f40ccc46dd43cfe60c05aa19bcc13936f5a1 Mon Sep 17 00:00:00 2001 From: Jun Hiroe Date: Sat, 12 Apr 2014 01:09:00 +0900 Subject: Add Array#rotate_bang --- mrbgems/mruby-array-ext/mrblib/array.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index f6adc5255..7f48811f1 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -382,4 +382,24 @@ class Array end ary end + + ## + # call-seq: + # ary.rotate!(count=1) -> ary + # + # Rotates +self+ in place so that the element at +count+ comes first, and + # returns +self+. + # + # If +count+ is negative then it rotates in the opposite direction, starting + # from the end of the array where +-1+ is the last element. + # + # a = [ "a", "b", "c", "d" ] + # a.rotate! #=> ["b", "c", "d", "a"] + # a #=> ["b", "c", "d", "a"] + # a.rotate!(2) #=> ["d", "a", "b", "c"] + # a.rotate!(-3) #=> ["a", "b", "c", "d"] + + def rotate!(count=1) + self.replace(self.rotate(count)) + end end -- cgit v1.2.3