I'm going through the code on a sample solution to my first Ruby Quiz (The Solitaire Cipher), and ran across this little nugget:
def move_down( index )
if index == @deck.length - 1
@deck[1..1] = @deck[index], @deck[1]
@deck.pop
else
...
end
end
The person who wrote this solution apparently used the multiple assignment in the second line to insert @deck[index] into the position before @deck[1]. Why not just use this?
@deck.insert(1, @deck[index])
Is there a difference?
a = [1,2,3];a[2] = a[0], a[1];a #=> [1, 2, [1, 2]]@deck.insert(1, @deck[index])is obvious, whereas the other code is too clever by half.