Instantiating classes / sharing data between classes

I think this is what I want to do (maybe I’m thinking about it wrong):

a = Doctor.new
b = Doctor.new

I want both ‘a’ and ‘b’ to have access to:
modalities = [‘US’, ‘CR’, ‘CT’, ‘MR’]

The array ‘modalities’ is not hard-coded, but generated from a
database query. I don’t want to query the database every time I create
a new instance of Doctor - so how can I do it so that every instance
of Doctor shares the same array, only created once?

Is this the part where ‘modules’ and ‘classes’ come into question and
interact?

create a class variable?
@@modalities ?

p.s. don’t know if ok…

2009/7/22 Trevoke [email protected]

Sounds like another class, Modalities. Could be a singleton, so each
doctor
can do

Modalities.get #=> [‘US’, ‘CR’, ‘CT’, ‘MR’]

and inside this class is where the database querying happens. Doctors
shouldn’t care where it comes from, just that it exists.

Jason

class Doctor
def modalities
@@modalities ||= your stuff
end
end

-chris

How about a constant?

class Doctor
MODALITIES = [‘US’, ‘CR’, ‘CT’, ‘MR’]

 def some_fun()
   'US' == MODALITIES[0]
 end

end

Regards,
Florian

On Jul 22, 12:39 pm, Jason R. [email protected] wrote:

Sounds like another class, Modalities. Could be a singleton, so each doctor
can do

Modalities.get #=> [‘US’, ‘CR’, ‘CT’, ‘MR’]

and inside this class is where the database querying happens. Doctors
shouldn’t care where it comes from, just that it exists.

Jason

Yeah… You’re right, it really should be another class. I shouldn’t
sacrifice clarity of code for expedience’s sake.

On Jul 22, 12:11 pm, Chris R. [email protected] wrote:

[Note: parts of this message were removed to make it a legal post.]

class Doctor
def modalities
@@modalities ||= your stuff
end
end

-chris

Ah… Understood… Thank you!

On Jul 22, 2009, at 12:11 PM, Chris R. 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 W.

On Jul 22, 1:26 pm, Gary W. [email protected] wrote:

  @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 W.

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?

Hi –

On Thu, 23 Jul 2009, Trevoke wrote:

Some alternatives that I would consider first:
  def self.modalities

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

On Jul 22, 2009, at 3:15 PM, Trevoke wrote:

In this particular instance, are class variables fulfilling their
destiny?

Here is a nice discussion of the confusion that surrounds
‘class variables’ and ‘class instance variables’ in Ruby:

<http://www.oreillynet.com/ruby/blog/2007/01/nubygems_dont_use_class_variab_1.html

Most people expect Ruby’s class variables to behave like
Java or C++'s class variables but that is an incorrect assumption.

In Ruby, “instance variables of a class” are most closely
analogous to ‘class variables’ in Java or C++ and should
generally be used instead of Ruby’s ‘class variables’.

Gary W.

Trevoke wrote:

I think this is what I want to do (maybe I’m thinking about it wrong):

a = Doctor.new
b = Doctor.new

I want both ‘a’ and ‘b’ to have access to:
modalities = [‘US’, ‘CR’, ‘CT’, ‘MR’]

Another option for you:

modalities = [‘US’, ‘CR’, ‘CT’, ‘MR’]
a = Doctor.new(modalities)
b = Doctor.new(modalities)

i.e. each has an instance variable pointing to the shared object.

If you like this approach, you can take it further - see
http://onestepback.org/index.cgi/Tech/Ruby/DependencyInjectionInRuby.rdoc

Unfortunately, Jim modified his site and didn’t carry forward the
attachment, and so the link to depinj.rb remains sadly dead. I think I
have a local copy floating around somewhere.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs