# Find elements of an array on another

**URL:** <https://discuss.rubyonrails.org/t/find-elements-of-an-array-on-another/17725>\
**Category:** rubyonrails-talk\
**Created:** [September 4, 2007, 1:21am UTC](https://discuss.rubyonrails.org/t/find-elements-of-an-array-on-another/17725 "2007-09-04T01:21:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carlos\_Fonseca](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@Carlos\_Fonseca](https://discuss.rubyonrails.org/u/Carlos_Fonseca)\
**Post date:** [September 4, 2007, 1:21am UTC](https://discuss.rubyonrails.org/t/find-elements-of-an-array-on-another/17725/1 "2007-09-04T01:21:24Z")

</div>

Hi

I have two arrays of items, and I want to cycle one and see if its items are on the other one, but I can't find out how to do it... I've been trying to do something like this

@a = Item.find(:all, :conditions =\> "name LIKE '%a%'") @b = Item.find(:all, :conditions =\> "name LIKE '%b%'")

for i in @a &nbsp;&nbsp;if @a.find(i) &nbsp;&nbsp;&nbsp;&nbsp;do stuff &nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;do something else &nbsp;&nbsp;end end

and I keep getting a "no block given" error on the if line... if @a is an array, i can search using find, and I believe I can use the object... I've tried a find\_by\_id but I got an "undefined method for array" error...

Any ideas? Thanks Carlos Fonseca

---

<div class="post-metadata">

**Author:** ![Rein\_Henrichs](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Rein\_Henrichs](https://discuss.rubyonrails.org/u/Rein_Henrichs)\
**Post date:** [September 4, 2007, 2:35am UTC](https://discuss.rubyonrails.org/t/find-elements-of-an-array-on-another/17725/2 "2007-09-04T02:35:08Z")

</div>

If I'm understanding you correctly, you're trying to check if each element in array @a is present in array @b and act accordingly, something like:

@a.each do |item| &nbsp;&nbsp;if @b.include? item &nbsp;&nbsp;&nbsp;&nbsp;[do stuff] &nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;[do other stuff] &nbsp;&nbsp;end end

It would be more performant (but less readable) to do something like this:

( @a & @b ).each do |item| &nbsp;&nbsp;[do stuff] end

( @a - @b ).each do |item| &nbsp;&nbsp;[do other stuff] end

---

<div class="post-metadata">

**Author:** ![Carlos\_Fonseca](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@Carlos\_Fonseca](https://discuss.rubyonrails.org/u/Carlos_Fonseca)\
**Post date:** [September 4, 2007, 10:57am UTC](https://discuss.rubyonrails.org/t/find-elements-of-an-array-on-another/17725/3 "2007-09-04T10:57:21Z")

</div>

I tried the .include?( item ) but it didn't work back then and I forgot to mention it yesterday. No idea why it wasn't working but now it works! Thanks 😃
