From: Brian Candler Date: 2003-08-09T17:14:14+09:00 Subject: Re: Ruby and OOP-design (question of an old "procedural person" ;) On Sat, Aug 09, 2003 at 04:47:10PM +0900, Meino Christian Cramer wrote: > Then I read about the new sort_by feature. This is exactly what I > need! > > But sort_by needs an array as input. > > And my "list"-object is an object (or a duck, which quaks ;) holding > and array...or what...or .... I got confused here. class List def sort! @list = @list.sort_by { |x| [x.freq, x.time] } end end In other words, since you've wrapped Array in your List object, then you give your List object a sort! method which sorts itself. This is an example of the "has_a" / "delegation" design pattern: List contains (has_a) Array instance, and List#sort passes on (delegates) the sort request to the object it contains. This is an excellent way of composing systems, but in this simple case you might just want to use an Array directly and drop the List class. It depends how much extra functionality you want to bundle in there. If there's not much in 'List' on top of a standard Array, an alternative is to make class methods which perform the specific operations you want on an array of your special objects: class Broadcaster def Broadcaster.sort_by_freq(arr) arr.sort_by { |x| [x.freq, x.time] } end end stations = [] # generic array stations << Broadcaster.new(...) ... stations = Broadcaster.sort_by_freq(stations) > Is this "design" ok (in the sense of OOP)? I'd say it looks pretty sound (no pun intended!) Regards, Brian.