On Fri, 8 Nov 2002, Booth, Peter wrote:

> I'm wondering if there is a more idiomatic way to do the following?
> Can Array.collect be used to extract a subset of an array?
>
> Peter
>
> Can
>
> class ProcessInfo < Array
>
> 	def findByRealm(realm)
> 		result = ProcessInfo.new
> 		each { |process|
> 			if process['realm'] == realm
> 		  		result << process
> 		  	end
> 		 }
> 		 return result
> 	end
>
> end

how about

class ProcessInfo < Array
  def findByRealm(realm)
    self.select {|process| process['realm'] == realm}
  end
end

the only problem with this is that returned value will be an Array.  without
seeing your initialize it's hard to tell if that will be a problem; if your
initialize can take an Array as an arg you could write

    ProcessInfo.new self.select {|process| process['realm'] == realm}

or you could redefine the select method - which would simply be a more general
'findByRealm'.  finally, you could specify a conversion method like
'ProcessInfo_from_Array' and use it to 'cast' the result of select.

-a

-- 

 ====================================
 | Ara Howard
 | NOAA Forecast Systems Laboratory
 | Information and Technology Services
 | Data Systems Group
 | R/FST 325 Broadway
 | Boulder, CO 80305-3328
 | Email: ahoward / fsl.noaa.gov
 | Phone:  303-497-7238
 | Fax:    303-497-7259
 ====================================