Method: Array#rotate
- Defined in:
- ext/enterprise_script_service/mruby/mrbgems/mruby-array-ext/mrblib/array.rb
#rotate(count = 1) ⇒ Object
call-seq:
ary.rotate(count=1) -> new_ary
Returns a new array by rotating +self+ so that the element at +count+ is
the first element of the new array.
If +count+ is negative then it rotates in the opposite direction, starting
from the end of +self+ where +-1+ is the last element.
a = [ "a", "b", "c", "d" ]
a.rotate #=> ["b", "c", "d", "a"]
a #=> ["a", "b", "c", "d"]
a.rotate(2) #=> ["c", "d", "a", "b"]
a.rotate(-3) #=> ["b", "c", "d", "a"]
427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-array-ext/mrblib/array.rb', line 427 def rotate(count=1) ary = [] len = self.length if len > 0 idx = (count < 0) ? (len - (~count % len) - 1) : (count % len) # rotate count len.times do ary << self[idx] idx += 1 idx = 0 if idx > len-1 end end ary end |