Find elements of an array on another


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
  if @a.find(i)
    do stuff
    do something else

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?
Carlos Fonseca

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|
  if @b.include? item
    [do stuff]
    [do other stuff]

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

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

( @a - @b ).each do |item|
  [ do other stuff ]

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 :smiley: