Stephen Wilhelm <swilhelm / theriver.com> wrote in message news:<a8qrt2$sk6$0 / 206.97.60.126>... > 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 <snip> > 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. The parse error you get on the 'alias :old_<< :<<' and 'alias :old_[]= :[]=' lines isn't because of the 2nd argument to alias (though it does look that way from the traceback), but because of the first: Ruby's not happy with either 'old_<<' or 'old_[]=' as method names. You'll have to cook up a different name for these aliases. > 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? I guess for performance - it'd really slow down array manipulation. Also, from an OO point of view, arrays don't really carry much 'meaning' - it might be better to observe a more meaningful class that just uses an array for internal storage. -- George