From: Louis J Scoras Date: 2006-11-26T09:06:43+09:00 Subject: Re: ruby and list comprehension On 11/25/06, Brad Tilley wrote: > In Python, I can do this to arrays: > > added = [x for x in new_data if x not in old_data] > removed = [x for x in old_data if x not in new_data] > same = [x for x in new_data if x in old_data] Short answer: added = new_data.reject {|i| old_data.include? i } removed = old_data.reject {|i| new_data.include? i } same = new_data.select {|i| old_data.include? i } Provided ordering isn't important here, you can do the same thing with set operations. require 'set' old_data = old_data.to_set new_data = new_data.to_set added = new_data - old_data removed = old_data - new_data same = new_data.intersection(old_data) Note those returns sets, not arrays. -- Lou.