swap two variables with Ruby

Say ‘a’ and ‘b’ are two variables assigned with some values. They do not need to be of the same data type (Ruby has dynamic typing, so you are safe ). If you want to swap these two variables, then this is the shortest way to do it.

a, b = b, a
How simple it is to do such a thing. It is convenient for developers to use shorthands like above. But how hard it is to understand the above line for a non-Ruby programmer.

I was in a deep trouble in understanding such code snippets written in Ruby, when i was learning Ruby. So I know the pain.

Advertisements

About this entry