Struct creates non-standard classes

M

Mark Volkmann

If Struct is a shorthand way for creating Classes, why don't objects
created from those Classes have instance_variables?

Struct.new 'Car', :make, :model, :year
c =3D Struct::Car.new 'Saturn', 'SC2', 1997
c.instance_variables =3D> []
c.members =3D> ["make", "model", "year"]

Note that members is an instance method of Struct, whereas
instance_variables is an instance method of Object.
 
D

David Vallner

D=C5=88a Nede=C4=BEa 12 Febru=C3=A1r 2006 20:45 Mark Volkmann nap=C3=ADsal:
If Struct is a shorthand way for creating Classes, why don't objects
created from those Classes have instance_variables?

My guess is because Struct directly accesses a C hashtable, instead of=20
registering instance variables in the interpreter. Not like it should matte=
r=20
unless you for some reason with to access the variables with reflection=20
instead of the accessors, which I can't imagine why you'd want to do.

David Vallner
 
D

David Vallner

D=F2a Nede=B5a 12 Febru=E1r 2006 21:48 Robert Klemme nap=EDsal:
You can.


IMHO you better get used to access instance variables through accessor
methods instead of directly - even inside another class. You can run
into numerous problems if you always directly access instance
variables. The fact that Ruby allows to do this (even from sub class
methods) does not mean that it's always the best thing to do.

The fact that there are instance variables is a mere detail of the
implementation. If you stick with this basic OO usage pattern and make
accessing instance variables directly the exception instead of the
rule there is no difference in using Struct created classes and
"normal" classes.

Amen.

I blame selfdot syndrome when using setters for people using instance=20
attributes directly. I wonder if we're getting that shorthand I saw mention=
ed=20
somewhere, if using accessors wasn't any longer than not using them, I expe=
ct=20
a dramatic increase of their use internally.

David Vallner
 
H

Hal Fulton

Mark said:
On 2/12/06, David Vallner <[email protected]> wrote:> D=F2a Nede=B5a 12=
Febru=E1r 2006 20:45 Mark Volkmann nap=EDsal:> > If Struct is a shorthan=
d way for creating Classes, why don't objects> > created from those Class=
es have instance_variables?> >>> My guess is because Struct directly acce=
sses a C hashtable, instead of> registering instance variables in the int=
erpreter. Not like it should matter> unless you for some reason with to a=
ccess the variables with reflection> instead of the accessors, which I ca=
n't imagine why you'd want to do.
My issue is that I wanted to think of Struct as an alternate way ofdefi=
ning a class and that after doing so I could use it like any otherclass. =
However, classes created using Struct aren't the same asclasses created i=
n the "normal" way and because of that I have toremember when I use them =
how they differ. I've found one way in whichthey differ and don't know if=
there are others.
--R. Mark VolkmannPartner, Object Computing, Inc.


I don't know why some posts come out like the above. But anyway:

This Struct issue was one reason I created SuperStruct (see
http://sstruct.rubyforge.org).

The usual disclaimer: I think it has bugs in spite of my testing.
If you find any, let me know.


Cheers,
Hal
 
H

Hal Fulton

Robert said:
2006/2/12, Mark Volkmann <[email protected]>:> On 2/12/06, Davi=
20:45 Mark Volkmann nap=EDsal:> > > If Struct is a shorthand way for crea=
ting Classes, why don't objects> > > created from those Classes have inst=
ance_variables?
fining a class and that after doing so I could use it like any other> cla=
ss.
=20
You can.
=20

Well, what if you want to add a new method to the class? That
method should have the right to use the instance variables.
But in a Struct there aren't any.


Hal
 
J

James Britt

Hal said:
Robert Klemme wrote:
=20
=20
Well, what if you want to add a new method to the class? That
method should have the right to use the instance variables.

It does, if there are any.
But in a Struct there aren't any.

Must a class have instance variables to be a 'real' class?

Why does the use of an array or hash, as the sole instance variable,=20
seem (if I'm following this correctly) to disqualify a class as a 'real'=20
class?

From the outside, objects simply respond to messages. On the inside,=20
they do stuff when sent a message. Sometimes the outside and the inside=20
match up (where you get the Ruby version of public properties), but=20
there's no reason they *have* to, and no reason to assume it is the case.

obj.foo=3Dx

should not lead one to think that obj implements an instance var @foo.


--=20
James Britt
 
H

Hal Fulton

James said:
Hal Fulton wrote:
=20
=20
=20
It does, if there are any.
=20
=20
=20
Must a class have instance variables to be a 'real' class?
=20
Why does the use of an array or hash, as the sole instance variable,=20
seem (if I'm following this correctly) to disqualify a class as a 'real= '=20
class?
=20
From the outside, objects simply respond to messages. On the inside,=20
they do stuff when sent a message. Sometimes the outside and the insid= e=20
match up (where you get the Ruby version of public properties), but=20
there's no reason they *have* to, and no reason to assume it is the cas= e.
=20
obj.foo=3Dx
=20
should not lead one to think that obj implements an instance var @foo.
=20

It doesn't have to, but it's common practice.

Once I wanted to construct a simple class, so I used Struct. Later I
decided I wanted to add a custom method to it. I discovered then that
the attributes did not correspond to instance variables. I had to
re-write the class instead of using a Struct.

Maybe I didn't have the "right" to expect those instance variables to
exist, but I think it's an understandable mistake.


Hal
 
D

David Vallner

D=C5=88a Pondelok 13 Febru=C3=A1r 2006 07:14 Hal Fulton nap=C3=ADsal:
Once I wanted to construct a simple class, so I used Struct. Later I
decided I wanted to add a custom method to it. I discovered then that
the attributes did not correspond to instance variables. I had to
re-write the class instead of using a Struct.

Hmm. Can you give an example where you absolutely need to access an instanc=
e=20
variable instead of using the getter / setter?

David Vallner
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top