From: Justin Collins Date: 2007-03-01T15:03:14+09:00 Subject: Re: Array comparison.. why does this work. Daniel Sheppard wrote: >> I was curious if it just interates through one array looking for that >> element in the second, or does it (internally) sort the >> array, iterate >> each array performing comparision and if a > b skipping the equality >> determination to save CPU cycles and doing a 'next'. >> > > I would guess that it would just do an include? call on the second array > and delete the element if it is true. > > Doing the sort first would probably not be faster - a sort operation is > not a cheap operation, and there is no guarantee that the members of an > array are mutually sortable anyway (["a", 1, Object.new] for example) - > there is no way in which the '-' method would be able to rely on calling > the '<=>' method on its contents. > > Dan. > Don't forget, you can always just look at the source[1]. It looks to me like it converts the second array to a hash, then iterates through the first array, checking if each element is in the newly created hash. If it is, it skips it. If not, the value goes in the new array. -Justin [1] http://ruby-doc.org/core/classes/Array.src/M002261.html