r/learnruby • • Jul 24 '20

Find same elements in arrays using inject(:&)

arr1 = [1,2,3]
arr2 = [2,3,4]
combined = [arr1,arr2]
combined.inject(:&) ---> [2,3]

I don't understand how it works. I found that snippet on a tutorial and I have tried to google it but nothing came up

1 Upvotes

2 comments sorted by

View all comments

Show parent comment

1

u/tanahtanah Jul 24 '20

Thanks! I thought the ampersand in that context is converting stuffs to object like in (&:some_methods).

So that's basically an AND bitwise operator and it's used similar to other operators such as sum inject(:+) or products inject (:*)