Forum: Ruby Struct creates non-standard classes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-02-12 20:45
(Received via mailing list)
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 = Struct::Car.new 'Saturn', 'SC2', 1997
c.instance_variables => []
c.members => ["make", "model", "year"]

Note that members is an instance method of Struct, whereas
instance_variables is an instance method of Object.
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-02-12 21:16
(Received via mailing list)
DÅ?a Nedeľa 12 Február 2006 20:45 Mark Volkmann napísal:
> 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
registering instance variables in the interpreter. Not like it should
matter
unless you for some reason with to access the variables with reflection
instead of the accessors, which I can't imagine why you'd want to do.

David Vallner
Fcd0e4a77335a364f15014590c612352?d=identicon&s=25 Aredridel (Guest)
on 2006-02-12 21:17
(Received via mailing list)
On Mon, 2006-02-13 at 04:45 +0900, Mark Volkmann wrote:
> 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 = Struct::Car.new 'Saturn', 'SC2', 1997
> c.instance_variables => []
> c.members => ["make", "model", "year"]
>
> Note that members is an instance method of Struct, whereas
> instance_variables is an instance method of Object.


Because they're not stored as instance variables.
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-02-12 21:26
(Received via mailing list)
On 2/12/06, David Vallner <david@vallner.net> wrote:
> Dòa Nedeµa 12 Február 2006 20:45 Mark Volkmann napísal:
> > 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
> registering instance variables in the interpreter. Not like it should matter
> unless you for some reason with to access the variables with reflection
> instead of the accessors, which I can't imagine why you'd want to do.

My issue is that I wanted to think of Struct as an alternate way of
defining a class and that after doing so I could use it like any other
class. However, classes created using Struct aren't the same as
classes created in the "normal" way and because of that I have to
remember when I use them how they differ. I've found one way in which
they differ and don't know if there are others.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-02-12 21:48
(Received via mailing list)
2006/2/12, Mark Volkmann <r.mark.volkmann@gmail.com>:
> On 2/12/06, David Vallner <david@vallner.net> wrote:
> > Dòa Nedeµa 12 Február 2006 20:45 Mark Volkmann napísal:
> > > If Struct is a shorthand way for creating Classes, why don't objects
> > > created from those Classes have instance_variables?

> My issue is that I wanted to think of Struct as an alternate way of
> defining a class and that after doing so I could use it like any other
> class.

You can.

> However, classes created using Struct aren't the same as
> classes created in the "normal" way and because of that I have to
> remember when I use them how they differ. I've found one way in which
> they differ and don't know if there are others.

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.

Kind regards

robert
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-02-12 23:51
(Received via mailing list)
Dòa Nedeµa 12 Február 2006 21:48 Robert Klemme napísal:
> You can.
> 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
attributes directly. I wonder if we're getting that shorthand I saw
mentioned
somewhere, if using accessors wasn't any longer than not using them, I
expect
a dramatic increase of their use internally.

David Vallner
C1bcb559f87f356698cfad9f6d630235?d=identicon&s=25 Hal Fulton (Guest)
on 2006-02-13 06:41
(Received via mailing list)
Mark Volkmann wrote:
> On 2/12/06, David Vallner <david@vallner.net> wrote:> Dòa Nedeµa 12 Február 2006 20:45 
Mark Volkmann napísal:> > 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> registering instance variables in the 
interpreter. Not like it should matter> unless you for some reason with to access the 
variables with reflection> instead of the accessors, which I can't imagine why you'd want 
to do.
> My issue is that I wanted to think of Struct as an alternate way ofdefining 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 in 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
C1bcb559f87f356698cfad9f6d630235?d=identicon&s=25 Hal Fulton (Guest)
on 2006-02-13 06:44
(Received via mailing list)
Robert Klemme wrote:
> 2006/2/12, Mark Volkmann <r.mark.volkmann@gmail.com>:> On 2/12/06, David Vallner 
<david@vallner.net> wrote:> > Dòa Nedeµa 12 Február 2006 20:45 Mark Volkmann napísal:> > > 
If Struct is a shorthand way for creating Classes, why don't objects> > > created from 
those Classes have instance_variables?
>
>>My issue is that I wanted to think of Struct as an alternate way of> defining a class 
and that after doing so I could use it like any other> class.
>
> You can.
>

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
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-02-13 07:01
(Received via mailing list)
Hal Fulton wrote:
>>> other> class.
>>
>>
>> You can.
>>
>
> 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,
seem (if I'm following this correctly) to disqualify a class as a 'real'
class?

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

  obj.foo=x

should not lead one to think that obj implements an instance var @foo.
C1bcb559f87f356698cfad9f6d630235?d=identicon&s=25 Hal Fulton (Guest)
on 2006-02-13 07:14
(Received via mailing list)
James Britt wrote:
>>>> My issue is that I wanted to think of Struct as an alternate way of>
>
> class?
>
>  From the outside, objects simply respond to messages. On the inside,
> they do stuff when sent a message.  Sometimes the outside and the inside
> match up (where you get the Ruby version of public properties), but
> there's no reason they *have* to, and no reason to assume it is the case.
>
>  obj.foo=x
>
> should not lead one to think that obj implements an instance var @foo.
>

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
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-02-13 18:26
(Received via mailing list)
DÅ?a Pondelok 13 Február 2006 07:14 Hal Fulton napísal:
> 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
instance
variable instead of using the getter / setter?

David Vallner
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-02-13 20:33
(Received via mailing list)
On 2/13/06, David Vallner <david@vallner.net> wrote:
> Dòa Pondelok 13 Február 2006 07:14 Hal Fulton napísal:
> > 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 instance
> variable instead of using the getter / setter?

I think the main use would be for writing tools that help Ruby
developers. For example, I might want to write a tool that inspects
Ruby objects in a running program. I might want to ask "What data does
this object hold?". In order to answer that question, I'd have to know
whether the object was created from a "normal" class or a struct.
This topic is locked and can not be replied to.