From: Mike Austin Date: 2006-11-26T09:20:07+09:00 Subject: Re: ruby and list comprehension 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] > > I believe this is known as list comprehension in Python. How is this done in > Ruby? > > Thanks, > Brad Because Ruby's select() returns a value, not just a boolean like in Smalltalk, you can do the following: [1,2,3].select { |x| ![2,3,4].include? x } [2,3,4].select { |x| ![1,2,3].include? x } [1,2,3].select { |x| [2,3,4].include? x } You can also use Array operators: [1,2,3] - [2,3,4] [2,3,4] - [1,2,3] [1,2,3] & [2,3,4] Mike