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 if @a.find(i) do stuff else do something else 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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] else [do other stuff] end end It would be more performant (but less readable) to do something like this: ( @a & @b ).each do |item| [do stuff] end ( @a - @b ).each do |item| [ do other stuff ] end On Sep 3, 8:21 pm, Carlos Fonseca <carlosefons...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > if @a.find(i) > do stuff > else > do something else > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 :D On Sep 4, 3:35 am, Rein Henrichs <rein.henri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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] > else > [do other stuff] > end > end > > It would be more performant (but less readable) to do something like > this: > > ( @a & @b ).each do |item| > [do stuff] > end > > ( @a - @b ).each do |item| > [ do other stuff ] > end > > On Sep 3, 8:21 pm, Carlos Fonseca <carlosefons...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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 > > if @a.find(i) > > do stuff > > else > > do something else > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---