What a Difference an Array Makes
Today, I checked out the Array Class on ruby docs to see if I could figure out how to quickly update a blocklist using a delta from the previous day’s list.
I felt like a hotshot when I was able to to reduce it to a one liner:
A = {1,2,3,4,5}
B = {3,5,6,7}
#Find values that exist in A but not in B
C = A.reject{|x| B.include?(x) }
C = {1,2,4}
#Find values that exist in B but not in A
E = B.reject{|x| A.include?(x) }
E = {6,7}
#Find values that exist in both A and B
D = A.collect{|x| B.include?(x) }
D = {3,5}
But then I came across a much easier way on Techotopia:
A = {1,2,3,4,5}
B = {3,5,6,7}
#Find values that exist in A but not in B
C = A - B
C = {1,2,4}
#Find values that exist in B but not in A
E = B - A
E = {6,7}
#Find values that exist in both A and B
D = A - B
D = {3,5}
Gotta love the simplicity!
Add comment April 19th, 2008