Ruby Tricks you Probably knew but I didn't: Array#&
July 31st, 2009
I had to intersect 2 arrays today. Basically I had 2 arrays and wanted to only the objects that both arrays contain. Yeah, I could have done some select or reject loop, but I stumbled upon…
[:a, :b, :c, :d] & [:a, :c, :e]
#=> [:a, :c]
Who knew? I think I should really use operator overloading more often, and just so pleasing using an operator for an operation. Go figure.
Leave a Reply