From: Phlip Date: 2009-04-06T10:34:20+09:00 Subject: Re: how to dynamically create search term for array.find_all Adam Akhtar wrote: > i want to be able to create a mehtod that will filter results in an > array. The conditions are known at run time so id like to be able to do > it on the fly / dynamically. In general, why not use ActiveRecord and sqlite3? Maybe you have other reasons to use it, and dynamic queries is what AR's find system is all about... > some_array.find_by {|x| x.category == :scifi OR x.category == :horror OR > x.category == :comedy } There's a very important principle in programming: After you write a unit test for a feature (you _are_ writing unit tests, aren't you?), and after you add the feature and pass the test, you then refactor to remove duplication, and make the code more DRY. When code obeys "Don't Repeat Yourself", it becomes easier to upgrade. For example, if you had first merged all the duplicate .category == codes, you would get this: cats = [:scifi, :horror, :comedy] some_array.find_by {|x| cats.include?(x.category) } And that's your fix - change cats. This example shows how DRY code is easer to extend and upgrade. This is counterintuitive, but I have yet to see a counterexample.