Let's suppose that we have arrays x = ['a', 'b', 'c'] and y. Is there an easy way to move, say, the second element of x, to y? So that in the end, x is ['a', 'c'] and y is ['b'].
4 Answers
A special code for this example. It might not work on your other arrays. Instead of actually moving element, let's take the old array apart and construct two new arrays.
x = ['a', 'b', 'c']
x, y = x.partition {|i| i != 'b'}
x # => ["a", "c"]
y # => ["b"]
The delete_at approach is likely better for your situation, but, you know, it's good to know alternatives :)
Comments
Yes. For a specific element:
y = []
y << x.delete('b')
For a specific index:
y = []
y << x.delete_at(1)
This kind of stuff is well documented, btw.