On Sat, Jan 22, 2005 at 12:23:15AM +0900, Richard Turner wrote: > On Sat, 2005-01-22 at 00:12 +0900, James Edward Gray II wrote: > > Yes, but less Javaish and more Rubyish. ;) We're not a wordy bunch > > and [] is our standard accessor, say you convey the same information, > > but type less. > > > Fair enough :) I'd shy away from it because to me (at least, at the > moment), it seems to imply enumeration. I'd be tempted to assume that > Section[] would 'fill' from index 0 so that, if there are any Sections, > there will certainly be Section[0]. Since the parameter is an ID, not > an index, it doesn't seem right. > I agree :) > On the other hand, Section{} sits fine in my head, since there's no > implied order there :) > > > > class Selection > > private_class_method :new > > > > def self.[]( id ) > > Section.new( > > # whatever... > > ) > > end > > end > > > > That help? > > > > James Edward Gray II > > > > Perfect! Thanks. I'd just stumbled onto the idea of > > class Section > class <<self > private :new > end > . > . > . > end > Another option is to use the :new method on the class to implement the class Section class << self # Called to get (or create) new instance def new(id) cached_instances()[id] || cached_instances()[id] = super end def cached_instances() @cache = {} unless @cache @cache end end # Called on new instance def initialize(id) puts "Creating a new instance" @id = id end end puts Section.new(10).inspect puts Section.new(11).inspect puts Section.new(10).inspect puts Section.new(11).inspect Output: Creating a new instance #<Section:0xb7eeb6c4 @id=10> Creating a new instance #<Section:0xb7eeb64c @id=11> #<Section:0xb7eeb6c4 @id=10> #<Section:0xb7eeb64c @id=11> This way you hide the factory pattern behind a "normal" constructor, which might be nice (I wish java had something like this without resorting to byte-code manipulation). //Anders -- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Anders Engströí aengstrom / gnejs.net . http://www.gnejs.net PGP-Key: ED010E7F . [Your mind is like an umbrella. It doesn't work unless you open it.]