D. Taylor Singletary
2006-Mar-16 22:46 UTC
[Rails] Arrays of Model Objects, Intersections, Object Identification... ?
Hi all, Hope you can help me with understanding how Ruby / Rails treats arrays full of objects. Let''s say I have to arrays of objects. Both are the same kinds of objects. tomatoes = Fruit.find(:all, :conditions => [ ''tomato = ?'', true], :limit => 10) fruits = Fruit.find(:all, :limit => 10) And I want to create an array of these objects called @my_fruits, but I don''t want the set to contain any duplicates. In the Ruby API documentation for arrays it shows you can use the | operator to perform a union between two arrays, throwing out duplicates. OK. That sounds good. That''s what I want. @my_fruits = tomatoes | fruits 98% of the time I''ve found this works in my tests. I don''t get duplicates. But I''ve had instances where I do in fact have a row repeat. Any ideas as to why the union operator doesn''t work in this case? In my real world (tm) example, I''m sorting the database rows randomly, and the data changes frequently. Both arrays contain the same types of objects. In entirely likely that two arrays can contain the same object. Is there a performance hit in building an each loop on the likely smaller of the array to test whether the other array ".include?"s the object? Would this be considered more reliable? Example: @my_fruits = fruits tomatoes.each do | fruit | @my_fruits.push(fruit) unless @my_fruits.include?(fruit) end Why? Thanks. D. Taylor Singletary, Reality Technician -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060316/c72add66/attachment.html
Liquid
2006-Mar-16 22:57 UTC
[Rails] Arrays of Model Objects, Intersections, Object Identification... ?
Hi.. Have you tried the uniq! method for arrays? (ruby 1.84) http://corelib.rubyonrails.org/classes/Array.html#M000452 Something like @myarray.uniq! Hope this works for you On 3/17/06, D. Taylor Singletary <taylorsingletary@gmail.com> wrote:> > > Hi all, > > Hope you can help me with understanding how Ruby / Rails treats arrays > full of objects. > > Let''s say I have to arrays of objects. Both are the same kinds of objects. > > tomatoes = Fruit.find(:all, :conditions => [ ''tomato = ?'', true], :limit > => 10) > fruits = Fruit.find(:all, :limit => 10) > > > And I want to create an array of these objects called @my_fruits, but I > don''t want the set to contain any duplicates. > > In the Ruby API documentation for arrays it shows you can use the | > operator to perform a union between two arrays, throwing out duplicates. > > OK. That sounds good. That''s what I want. > > @my_fruits = tomatoes | fruits > > 98% of the time I''ve found this works in my tests. I don''t get duplicates. > But I''ve had instances where I do in fact have a row repeat. > > Any ideas as to why the union operator doesn''t work in this case? > > In my real world (tm) example, I''m sorting the database rows randomly, and > the data changes frequently. Both arrays contain the same types of objects. > In entirely likely that two arrays can contain the same object. > > Is there a performance hit in building an each loop on the likely smaller > of the array to test whether the other array ".include?"s the object? Would > this be considered more reliable? > > Example: > @my_fruits = fruits > tomatoes.each do | fruit | > @my_fruits.push(fruit) unless @my_fruits.include?(fruit) > end > > Why? > > Thanks. > D. Taylor Singletary, > Reality Technician > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060316/b42f3363/attachment.html
D. Taylor Singletary
2006-Mar-20 15:35 UTC
[Rails] Arrays of Model Objects, Intersections, Object Identification... ?
I figured this out in a duh moment this morning. One array was formed through traditional ActiveRecord find methods and the other was constructed with a find_by_sql string that included a join table in the select. The inclusion of the join table made the objects appear as different between the two sets. Sanity reset. Thanks for the suggestions. D. Taylor Singletary, Reality Technician On 3/16/06, Liquid <has.sox@gmail.com> wrote:> > Hi.. > > Have you tried the uniq! method for arrays? (ruby 1.84) > http://corelib.rubyonrails.org/classes/Array.html#M000452 > > Something like > > @myarray.uniq! > > Hope this works for you > > > On 3/17/06, D. Taylor Singletary <taylorsingletary@gmail.com> wrote: > > > > Hi all, > > Hope you can help me with understanding how Ruby / Rails treats arrays > full of objects. > > Let''s say I have to arrays of objects. Both are the same kinds of objects. > > tomatoes = Fruit.find(:all, :conditions => [ ''tomato = ?'', true], :limit > => 10) > fruits = Fruit.find(:all, :limit => 10) > > > And I want to create an array of these objects called @my_fruits, but I > don''t want the set to contain any duplicates. > > In the Ruby API documentation for arrays it shows you can use the | > operator to perform a union between two arrays, throwing out duplicates. > > OK. That sounds good. That''s what I want. > > @my_fruits = tomatoes | fruits > > 98% of the time I''ve found this works in my tests. I don''t get duplicates. > But I''ve had instances where I do in fact have a row repeat. > > Any ideas as to why the union operator doesn''t work in this case? > > In my real world (tm) example, I''m sorting the database rows randomly, and > the data changes frequently. Both arrays contain the same types of objects. > In entirely likely that two arrays can contain the same object. > > Is there a performance hit in building an each loop on the likely smaller > of the array to test whether the other array ".include?"s the object? Would > this be considered more reliable? > > Example: > @my_fruits = fruits > tomatoes.each do | fruit | > @my_fruits.push(fruit) unless @my_fruits.include?(fruit) > end > > Why? > > Thanks. > D. Taylor Singletary, > Reality Technician > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060320/b2c22adc/attachment.html