In Ruby, you can easily swap values of two variables without the need for a temporary third variable:
x,y = y,x
This is called ‘Parallel Assignment’.
Can’t believe it? I had to test it myself, too:
Can’t believe it? I had to test it myself, too:
$ irb
>> x = 5
=> 5
>> y = 10
=> 10
>> x,y = y,x
=> [10, 5]
>> x
=> 10
>> y
=> 5
No comments:
Post a Comment