Hi,

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.

For example, suppose I have a have an array of Albums, like this:

class Album
  attr_reader :name, :band, :like
  attr_writer :like

  def initialize(name, band)
    @name = name
    @band = band
    @like = nil
  end
end

albums = []
albums << Album.new("Exile on Main Street", "The Rolling Stones")
albums << Album.new("St. Peppers", "The Beatles")
albums << Album.new("The White Album", "The Beatles")

I want to be able to selectively modify the elements of the albums array
based on, for example, the band.  In other words, I want to be able to
able to change the like instance variable in each object to true with a
method.

Here's what I have so far:

puts albums.collect { |a| a.like = true if a.band == "The Beatles";
a}.inspect

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.  I'd like to be able just to select the elements
I want, make the changes, and leave the rest of the elements unchanged.
Is there a better way to do this?
-- 
Posted via http://www.ruby-forum.com/.