Today I Learned

hashrocket A Hashrocket project

Ruby Array Set Intersection

Ruby's Array class has a method & which returns a new array containing elements common to two arrays, excluding duplicates.

I used this on a project today to compare a random number (17) to a set of numbers with special meaning to the program.

2.2.2 :001 > (5..32).step(3).to_a & [17]
 => [17]
2.2.2 :002 > ((5..32).step(3).to_a & [17]).size > 0
 => true

The order is preserved from the original array:

2.2.2 :003 > [2,3,1] & [1,2,3]
 => [2, 3, 1]

h/t Chris Erin

See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.