3 ms·
Also, it depends on which language you're using... $ irb irb(main):001:0> a = 1 => 1 irb(main):002:0> b = 2 => 2 irb(main):003:0> b, a
by steveklabnik 14y ago
Also, it depends on which language you're using...
$ irb
irb(main):001:0> a = 1
=> 1
irb(main):002:0> b = 2
=> 2
irb(main):003:0> b, a = a, b
=> [1, 2]
irb(main):004:0> puts "#{a} #{b}"
2 1
=> nil
(obviously this has the same flaw you mention, the interpreter is doing stuff for you. It follows the letter of the 'no intermediate variables' only.)
- michaelfairley 14y agoYou can also follow the letter in C, Java, etc.: a ^= b b ^= a a ^= b
- deleted 14y ago[deleted]
- jervisfm 14y agoWhat does that do?
- steveklabnik 14y agohttp://en.wikipedia.org/wiki/XOR_swap_algorithm http://en.wikipedia.org/wiki/XOR_swap_algorithm
- jervisfm 14y agoThanks for the pointer.