From: "David A. Black" Date: 2009-07-23T05:35:38+09:00 Subject: Re: Instantiating classes / sharing data between classes --1926193751-1990285950-1248294818=:20487 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-1990285950-1248294818=:20487" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-1990285950-1248294818=:20487 Content-Type: TEXT/PLAIN; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8BIT Hi -- On Thu, 23 Jul 2009, Trevoke wrote: > On Jul 22, 1:26�pm, Gary Wright wrote: >> On Jul 22, 2009, at 12:11 PM, Chris Rhoden wrote: >> >>> class Doctor >>> �def modalities >>> � �@@modalities ||= your stuff >>> �end >>> end >> >> I think reaching for class variables is almost always a mistake. >> Some alternatives that I would consider first: >> >> a) Use a constant: >> >> class Doctor >> � �Modalities = query_for_modalities >> end >> >> b) Use a class method and lazy load the data >> >> class Doctor >> � �def self.modalities >> � � � @modalities ||= query_for_modalities >> � �end >> end >> >> I don't recommend class variables because their actual scope >> is very non-intuitive (class hierarchy scope, not class scope) >> and they add to the semantic load of a design when plain >> old instance variables (on the class object) will do just fine. >> >> Gary Wright > > Gary -- these values ARE the same for EVERY SINGLE INSTANCE of the > class, and are not meant to be changed. > In this particular instance, are class variables fulfilling their > destiny? It sounds like a job for a constant. David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) Training! Intro to Ruby, with Black & Kastner, September 14-17 (More info: http://rubyurl.com/vmzN) --1926193751-1990285950-1248294818=:20487-- --1926193751-1990285950-1248294818=:20487--