From: Lionel Bouton Date: 2007-07-28T05:05:17+09:00 Subject: Re: How to pass arguments by reference in a function Peter Seebach wrote the following on 27.07.2007 21:16 : > The xor swap is virtually never more efficient on reasonable hardware. > A casual pass through suggests that, even for the special case of numbers > where a, b, and a^b are all Fixnum-ranged, the straight swap ought to be > noticably faster. > > More importantly: How could doing it that way ever be idiomatic or easy > to understand? I cannot conceive of a program where I have to swap two > objects quickly, and the need to swap them quickly is so great that a hack > like that would be worth it... and I can't just bypass the swap by improving > the algorithm. > A program in assembler for a microcontroller with so few memory that using the stack for a temporary variable is out of the question comes to my mind. But for sure no program written in Ruby would need this kind of hack. Even more, if my assumptions of how Ruby internals work are correct, using the xor in this case is less memory efficient as it should generate new Fixnum objects instead of just swapping references to them... Lionel.