From: James Coglan Date: 2008-07-07T21:54:50+09:00 Subject: Re: inheritance v passing object through the constructor ------=_Part_25479_29300688.1215435505810 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 2008/7/7 : > box ||= Box.new >> class ErrorBox >> def initialize(box) >> end >> end >> > > That won't do anything as written; the two 'box' local variables are > distinct. > I'm guessing you mean something like this: > class ErrorBox > def initialize(box) > @box = box > end > def draw > @box.draw > end > end > eb = ErrorBox.new(Box.new) > eb.draw If ErrorBox really is a more specialized kind of Box, then subclassing makes sense in terms of design. If it's not really a new kind of Box but wants to use some of Box's behaviour then use delegation or consider mixins. Reg Braithewaite has a decent article on this topic: http://weblog.raganwald.com/2008/04/is-strictly-equivalent-to.html Also, seek out the decorator in a book on design patterns. It's usually presented as a way of extending the behaviour of objects in many orthogonal ways in situations where subclassing would result in combinatorial explosions of new subclasses. This is another case where mixins can also make sense. ------=_Part_25479_29300688.1215435505810--