From: Stephen Wilhelm Date: 2002-04-10T10:30:33+09:00 Subject: Observable Array Hello everyone. I have a need for an observable array, but I wanted my solution to be as general as possible, so I came up with the following modification to Class: class Class def notifiers( *symbols ) symbols.each do |symbol| class_eval <<-EOS alias :old_#{symbol.id2name} :#{symbol.id2name} private :old_#{symbol.id2name} def #{symbol.id2name}( *args ) ret = old_#{symbol.id2name}( *args ) changed( true ) notify_observers( self, "#{symbol.id2name}" ) ret end EOS end end end Basically, this modification adds a notifiers method that will, for each method symbol add a hook to notify all observers that the object has been modified. I then change Array to: class Array include Observable notifiers :clear, :collect!, :compact!, :concat, :delete, :delete_at, :delete_if, :fill, :flatten!, :map!, :pop, :push, :reject!, :replace, :reverse!, :shift, :slice!, :sort!, :uniq!, :unshift end Unfortunately, my code will not work with two methods: << and []=. << isn't much of a problem since its the same as push. But []= is much more problematic. For some reason having to do with the alias, it won't let me say "notifiers :[]=" or anything else I could think of, and there isn't an alias for []= that I can use. Without []=, my observable array just doesn't work. Overriding the definition of []= is problematic, since I don't know how to get at the previous definition wihtout using an alias. I could, of course, just create an ObservableArray class and use super with []=, but I am hoping that someone here could come up with a better solution, ideally one which will allow me to send operators to my notifiers method (which works pretty well, other than that). Thanks in advance. As an aside, it always seemed to me that Arrays would be the best use of the observer pattern, but no language I've seen does it or even makes it easy. Why not? -- Stephen Wilhelm