From: James Edward Gray II Date: 2005-01-22T00:12:05+09:00 Subject: Re: Refernce objects On Jan 21, 2005, at 8:58 AM, Richard Turner wrote: >> class Section >> def self.[](n) >> # here, a fetch command for record n >> end >> end >> >> record = Section[10] >> >> That would bypass the need for a constructor (if that's appropriate in >> your case). >> > > That's great, but isn't that just a more succinct way of doing the same > thing? I.e. isn't this the same?: > > class Section > def self.loadedFromDB(sectionID) > # here, a fetch command for record sectionID > end > end 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. > What I want []() or loadedFromDB() to return is a Section, but I don't > want people to be able to call Section.new in error, otherwise they'll > not get what they think. E.g.: class Selection private_class_method :new def self.[]( id ) Section.new( # whatever... ) end end That help? James Edward Gray II