mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge pull request #2047 from suzukaze/add-array-rotate_bang
Add Array#rotate!
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user