1

I have a ruby array:

["A", "C", "B", "D", "F", "E"]

User will supply an input, e.g.

input = "B"

I want to shift the values in the array, so the first item of the array equals input, and get the result of a new array:

["B", "D", "F", "E", "A", "C"]

User will be choosing from a dropdown options, so they can only choose the letters from original array.

0

1 Answer 1

5

You can use Array#rotate.

arr = ["A", "C", "B", "D", "F", "E"]

arr.rotate(arr.index('B'))
 #=> ["B", "D", "F", "E", "A", "C"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.