determining the attribute names of an object

M

Mark Volkmann

The way to determine the names of the attributes of objects created
from a Class, Struct and OpenStruct are all different.

For an object created from a Class you call "instance_variables". I
wish that method would have been named "attributes".

For an object created from a Struct you call "members". Why not the
same as the previous case?

For an object created using OpenStruct you call "marshal_dump.keys".
My first guess was to call "table.keys", but the table attribute is
protected.

I think there should be a common way to do this for all three cases.

Suggestion #1
- add an "instance_variables" method to Struct that is the same as "members=
"
- add an "instance_variables" method to OpenStruct that returns @table.keys

Suggestion #2
- add an "attributes" method to Class that is the same as "instance_variabl=
es"
- add an "attributes" method to Struct that is the same as "members"
- add an "attributes" method to OpenStruct that returns @table.keys

--=20
R. Mark Volkmann
Partner, Object Computing, Inc.
 
D

David A. Black

Hi --

The way to determine the names of the attributes of objects created
from a Class, Struct and OpenStruct are all different.

For an object created from a Class you call "instance_variables". I
wish that method would have been named "attributes".

instance variables are not the same as attributes. Attributes can be,
and often are, implemented by using instance variables to hold state.
But there is not a one-to-one relation. Even if you define
"attribute" strictly to mean methods defined with the attr_* family,
there still may be instance variables in use that are not connected
with those methods.
For an object created from a Struct you call "members". Why not the
same as the previous case?

There's no connection:

irb(main):001:0> S = Struct.new("S",:x)
=> Struct::S
irb(main):002:0> s = S.new
=> #<struct Struct::S x=nil>
irb(main):003:0> s.x = 1
=> 1
irb(main):004:0> s.instance_variables
=> []
irb(main):005:0> s.members
=> ["x"]


Instance variables don't map one-to-one to any higher-level concept
like attributes or members.


David
 
M

Mark Volkmann

instance variables are not the same as attributes. Attributes can be,
and often are, implemented by using instance variables to hold state.
But there is not a one-to-one relation. Even if you define
"attribute" strictly to mean methods defined with the attr_* family,
there still may be instance variables in use that are not connected
with those methods.

I really don't get this. Can you give a definition of each of the
terms "instance variable" and "attribute"? I'm pretty sure most
people that have been doing OO development for years would say that
they thought those were the same thing. Maybe many of us were sleeping
in class when those concepts were taught. ;-)

Are you saying that instance variables are all the things that start
with "@" that have been assigned a value within an object?

Are you saying that attributes are implied by the existence of get and
perhaps set methods?
For an object created from a Struct you call "members". Why not the
same as the previous case?
=20
There's no connection:
=20
irb(main):001:0> S =3D Struct.new("S",:x)
=3D> Struct::S
irb(main):002:0> s =3D S.new
=3D> #<struct Struct::S x=3Dnil>
irb(main):003:0> s.x =3D 1
=3D> 1
irb(main):004:0> s.instance_variables
=3D> []
irb(main):005:0> s.members
=3D> ["x"]
=20
Instance variables don't map one-to-one to any higher-level concept
like attributes or members.

But conceptually what is the difference between members of a Struct
and attributes of an object? And conceptually what is the difference
between the members of a Struct and the members of an OpenStruct? Why
would their be a different way to discover them?

--=20
R. Mark Volkmann
Partner, Object Computing, Inc.
 
D

David A. Black

Hi --

Ah ... thanks for the explanation! Had I been paying attention, I
would have understood the distinction between attributes and instance
variables from reading pages 32-33 in Pickaxe 2.

That still leaves the question of why Structs have "members" instead
of "instance variables" and why OpenStructs have neither.

If anything, I would say members correspond to attributes, not to
instance variables. But really neither. A Struct is just a class
that chooses to describe some of its capabilities as members. These
can coexist with attributes:

irb(main):001:0> S = Struct.new("S",:a,:b)
=> Struct::S
irb(main):002:0> class S; attr_accessor :x; end
=> nil
irb(main):003:0> s = S.new
=> #<struct Struct::S a=nil, b=nil>
irb(main):004:0> s.x = 1
=> 1
irb(main):006:0> s.instance_variables
=> ["@x"]
irb(main):007:0> s.members
=> ["a", "b"]


There's some overlap in functionality (just as their is between
'method' and 'attribute'), but I think it's probably good to have a
different name for the very specialized thing that Structs have, which
is created and implemented differently from attributes in the attr_*
sense.


David
 
J

Julian Leviston

Yeah,

Instance variables are a concern of the developer of the object, but
attributes are a concern of the user of the object.

Julian.
 

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

Forum statistics

Threads
473,787
Messages
2,569,627
Members
45,328
Latest member
66Teonna9

Latest Threads

Top