< All Blog
November 15, 2022
In Ruby, there is a way to swap values of two different variables without using temporary variables.
Let's say there are two variables that we'd like to swap.
a = 100
b = 200
The naive way to implement swap(x, y)
is using tmp
variable to store either of variable values temporary.
tmp = a
a = b
b = tmp
But if you use Ruby's parallel assignments, the above code can be written as follows:
a, b = b, a