Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move first element to the end of array

Tags:

arrays

ruby

What is the best way to move the first element of the array till the end of this same array?

ie: [a,b,c,d]

"Some operation"

result: [b,c,d,a]

What should this "Some operation" be?

like image 514
necker Avatar asked Jun 29 '13 10:06

necker


People also ask

How do you move position of element in array?

To change the position of an element in an array:Use the splice() method to insert the element at the new index in the array. The splice method changes the original array by removing or replacing existing elements, or adding new elements at a specific index.

How do you get to the end of an array?

1) Using the array length property The length property returns the number of elements in an array. Subtracting 1 from the length of an array gives the index of the last element of an array using which the last element can be accessed.


1 Answers

There is Array#rotate:

[a,b,c,d].rotate(1)
like image 189
sawa Avatar answered Sep 19 '22 13:09

sawa