From: dblack@... Date: 2007-03-01T11:57:10+09:00 Subject: Re: Array comparison.. why does this work. Hi -- On Thu, 1 Mar 2007, Jean Nibee wrote: > How does Ruby do Array comparision? One element at a time. > [code] > old_inventory = [ 'a', 'b', 'd' ] > new_inventory = [ 'a','e', 'c' ] > > puts "The following files have been added: " > puts new_inventory - old_inventory > puts "" > puts "The following files have been deleted: " > puts old_inventory - new_inventory > [/code] > > Notice the output is correct so it's not a case of element 0's being > compared in each array. > > So... > > Does Ruby sort arrays in memory first? or is a scan done 1 element at a > time? You're not doing comparison, though; you're doing subtraction. Subtraction doesn't depend on order. You can think of it as "without": [1,2,3] - [1] => [2,3] # [1,2,3] without 1 [1,2,3,1] - [1] => [2,3] # [1,2,3,1] without 1 [1,2,3] - [2] => [1,3] # [1,2,3] without 2 etc. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)