Taken from: http://www.ruby-forum.com/topic/89416
Author: Phrogz
To avoid reinventing the wheel check out the built-in methods described in:
http://www.ruby-doc.org/core/classes/Array.html and
http://www.ruby-doc.org/core/classes/Enumerable.html.
module Enumerable
def comprehend(&block)
block ? map(&block).compact : self
end
end
old_data = *(1..5)
new_data = *(3..9)
added = new_data.comprehend { |x| x if not old_data.include?(x) }
removed = old_data.comprehend { |x| x if not new_data.include?(x) }
same = new_data.comprehend { |x| x if old_data.include?(x) }
modified = new_data.comprehend { |x| x**2 if not x % 2 == 0 }
p added
p added = new_data - old_data
p removed
p removed = old_data - new_data
p same
p same = old_data & new_data
p modified
new_data = [[5, 9], [22, 3], [99, 564]]
bool_vals = new_data.comprehend { |x, y| x <= y }
p bool_vals