Comments on: Intersection of Array of Arrays – Ruby http://pullmonkey.com/2008/03/31/intersection-of-array-of-arrays-in-ruby/ Sun, 30 Aug 2009 04:45:40 +0000 hourly 1 https://wordpress.org/?v=5.6 By: kourge http://pullmonkey.com/2008/03/31/intersection-of-array-of-arrays-in-ruby/comment-page-1/#comment-375 Mon, 31 Mar 2008 18:26:00 +0000 /2008/03/31/intersection-of-array-of-arrays-in-ruby#comment-375 Converting the arrays to strings using to_json, then forcing the interpreter to reevaluate them is slow.
The faster way is:
Tag.find(params[:tag_ids]).map(&:article_ids).inject {|x, y| x & y }

If you’re using Ruby 1.9, it can be even shorter because inject can take a symbol for a method name:
Tag.find(params[:tag_ids]).map(&:article_ids).inject(:"&")

]]>
By: charlie http://pullmonkey.com/2008/03/31/intersection-of-array-of-arrays-in-ruby/comment-page-1/#comment-376 Mon, 31 Mar 2008 18:26:00 +0000 /2008/03/31/intersection-of-array-of-arrays-in-ruby#comment-376 @kourge – thanks a ton, that is perfect, you’re the man!

]]>