Instantiating classes / sharing data between classes

T

Trevoke

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?
 
M

Mirko Viviani

create a class variable?
@@modalities ?

p.s. don't know if ok...

2009/7/22 Trevoke said:
I think this is what I want to do (maybe I'm thinking about it wrong):

a =3D Doctor.new
b =3D Doctor.new

I want both 'a' and 'b' to have access to:
modalities =3D ['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?


--=20
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"=93Machines take me by surprise with great frequency.=94 A. Turing
 
C

Chris Rhoden

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

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

-chris
 
J

Jason Roelofs

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

Modalities.get #=3D> ['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

create a class variable?
@@modalities ?

p.s. don't know if ok...

2009/7/22 Trevoke said:
I think this is what I want to do (maybe I'm thinking about it wrong):

a =3D Doctor.new
b =3D Doctor.new

I want both 'a' and 'b' to have access to:
modalities =3D ['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?


--
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"=93Machines take me by surprise with great frequency.=94 A. Turing
 
F

Florian Gilcher

How about a constant?

class Doctor
MODALITIES = ['US', 'CR', 'CT', 'MR']

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

Regards,
Florian
 
G

Gary Wright

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
 
T

Trevoke

[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!
 
T

Trevoke

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.
 
T

Trevoke

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?
 
G

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?


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 Wright
 
B

Brian Candler

Trevoke said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top