Tuesday, January 6, 2015

Ruby array comparison

To compare 2 arrays in ruby, you can take the intersection of the two and see if it's empty:

a=[1,2,3,4]
b=[2,4,6,7,8]

if (a & b).empty?
puts 'Nothing in common'
else
 puts 'Common elements:'
 puts (a & b)
end




----- output ------
Common elements:
2
4