From: Ron Jeffries Date: 2002-02-26T03:24:18+09:00 Subject: Re: Style Question On Mon, 25 Feb 2002 16:24:18 GMT, Dave Thomas wrote: >Ron Jeffries writes: > >> I could build a separate class, SetFactory, and do things over there, >> but in Ruby I see no reason to do that. I'm comfortable with a bunch of >> factory methods on Set, such as Set.create, except that for those >> methods to compile, all the concrete subclasses that Set uses have to be >> at least declared before they are used. So the top of the code looks >> like this: > >How about having the concrete classes extend set to add their own >factories in > > # untested > class Set > class < protected :new > end > end I don't understand the notation above with class << etc. What's up with that? How is it different from class Set protected :new end > class ArraySet < Set > def Set.fromArray(ary) > s = Set.new > ary.each {|i| s.add(i) } > end > end > > class WibbleSet < Set > def Set.fromWibble(wib) > # ... > end > end I think I get this. It's basically the same deal but avoids having to predeclare the subclasses up at the top, right? Definitely a better way. > > > as = Set.fromArray [ 1, 2, 3, 4 ] > wb = Set.fromWibble( wibble, wobble, whee! ) > >This schema means you can add new concrete representations without >ever changing the base class. I guess you mean without ever editing the base class's file if it has its own. Certainly defining a method on it is changing the class itself? Thanks! Ronald E Jeffries http://www.XProgramming.com http://www.objectmentor.com I'm giving the best advice I have. You get to decide whether it's true for you.