From: Rob Redmon Date: 2009-05-01T20:12:57+09:00 Subject: Re: Finding Matches in Array of Classes snex wrote: > On Apr 29, 2:24�pm, Todd Benson wrote: >> >> The class of "observation_time" is: >> >> What's the ruby way? �Obviously, I can't use Array.include? but >> beware, though, since (d...e).include?(e) is false, but >> (d...e).include?(d) is true. >> >> Todd > > shouldnt you do this kind of filtering on the ActiveRecord#find call? First, thank you for the varied and interesting suggestions. I always learn a lot here! My motivation is to present an inventory summary for scientific observations. A sample is attached. From that motivation, I got thinking about array processing in general and thus my question. To create this view, I am greedily calling ActiveRecord#find many times to essentially work with the same (24 * 366) rows. To me one call to retrieve all of these rows and then pivot and filter them in local memory makes more sense; it would be especially cool if I could use the same "find" like calls without regard to whether the rows really came from an AR connection or an already available resulset. Being used to scientific analysis programming where we are always dealing with multidimensional arrays, I wondered, isn't there a mechanism to operate on an entire array, filtering on components which are "comparable"? Consider the following IDL as pseudocode (Matlab would be similar). This filter which gives data in the time range of "1990-1-1 12:00:00" to "1990-1-1 12:59:59" would look like: indices = where( data.observation_time >= julday( 1, 1, 1990, 12, 0, 0) ) and data.observation_time < julday( 1, 1, 1990, 13, 0, 0 ) ) However, it seems from these examples, I must implement this functionality myself (when it becomes necessary). It seems to me I'll need to write a function for each possible expression I might have which essentially just iterates and performs that expression. Consider a compound filter like (again in IDL): indices = where( data.quality eq "good" and data.observation_time >= julday( 1, 1, 1990, 12, 0, 0) ) and data.observation_time < julday( 1, 1, 1990, 13, 0, 0 ) ) This certainly maps very well to an ActiveRecord#find statement. But what if I already have my resultset available? Or what if I'm not using a database at all and I simply have an array of objects? For now, in this database example, I am using ActiveRecord#find, but this situation had me wondering about Ruby Array filtering in general. I recall way back when I was in school and programming C++ for engineering analysis. I wrote a sparse matrix library which overloaded "<" and "&&" to yield the functionality above. Aren't usual comparison tokens already overloaded somewhere to operate on arrays? Thanks again for the great suggestions!! Attachments: http://www.ruby-forum.com/attachment/3637/BC840_1980_SAO.html.zip -- Posted via http://www.ruby-forum.com/.