diff options
| author | Jun Hiroe <[email protected]> | 2014-04-12 01:09:00 +0900 |
|---|---|---|
| committer | Jun Hiroe <[email protected]> | 2014-04-12 01:19:44 +0900 |
| commit | 0898f40ccc46dd43cfe60c05aa19bcc13936f5a1 (patch) | |
| tree | 2823ac328e0580730d2f1683258a516f3965b7c1 | |
| parent | fe55b9dd1cca607398454bd2d432b2482f40cbef (diff) | |
| download | mruby-0898f40ccc46dd43cfe60c05aa19bcc13936f5a1.tar.gz mruby-0898f40ccc46dd43cfe60c05aa19bcc13936f5a1.zip | |
Add Array#rotate_bang
| -rw-r--r-- | mrbgems/mruby-array-ext/mrblib/array.rb | 20 | ||||
| -rw-r--r-- | mrbgems/mruby-array-ext/test/array.rb | 10 |
2 files changed, 30 insertions, 0 deletions
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 diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 650e49642..ab7a24df7 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -156,3 +156,13 @@ assert("Array#rotate") do assert_equal ["c", "d", "a", "b"], a.rotate(10) assert_equal [], [].rotate end + +assert("Array#rotate!") do + a = ["a", "b", "c", "d"] + assert_equal ["b", "c", "d", "a"], a.rotate! + assert_equal ["b", "c", "d", "a"], a + assert_equal ["d", "a", "b", "c"], a.rotate!(2) + assert_equal ["a", "b", "c", "d"], a.rotate!(-3) + assert_equal ["c", "d", "a", "b"], a.rotate(10) + assert_equal [], [].rotate! +end |
