On Oct 19, 2007, at 10:20 AM, aa wrote:

>
> I'm implementing a plain simple search functionality over an array of
> objects. One of the attributes that those objects expose is a creation
> date (a Time object). The search functionality should work in 3 ways:
> - starting from a date
> - up to a date
> - within a certain range
>
> In the first implementation I had the same code repeated in different
> search methods:
>
>   def search_from(start)
>     @list.select {|s| s.creation >= start}
>   end
>   def search_to(_end)
>     @list.select {|s| s.creation <= _end}
>   end
>   def search(start, _end)
>     @list.select {|s| s.creation >= start && s.creation <= _end}
>   end
>

why not something simple like

def search &filter
   @list.select &filter
end

def search_from time
   search{|s| s.creation and s.creation >= time}
end

def search_in time_range
   search{|s| s.creation and time_range === s.creation}
end

def search_until time
   search{|s| s.creation and s.creation <= time}
end

??

a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being  
better. simply reflect on that.
h.h. the 14th dalai lama