From: Phrogz Date: 2010-03-21T06:20:07+09:00 Subject: Re: Modifying Select Elements in Array of Objects On Mar 20, 3:07 pm, Glenn Ritz wrote: > I have an array of objects, and the objects have some attributes.  I'd > like to be able to modify selected elements in the array based on any of > those attributes. See Array#select and Array#find. e.g. albums.select{ |a| a.band=="The Beatles" }.each{ |a| a.like=true } > Using collect like this works, but it seems awkward to me to have to > iterate through the whole array just to find the element (or elements) > that I want to change. Without some previously-established alternative lookup mechanism, such as a Hash, any implementation is going to have to walk through the entire array to find multiple matching items. Array#find will stop as soon as it finds one item, though. For example (assuming you still need your albums array): # Do this once albums_by_band = albums.group_by{ |a| a.band } # Do this for each time you need to look up an array of albums by a band albums_by_band["The Beatles"].each{ |a| a.like = true }