Hi --

On Wed, 24 Aug 2005, Mark Volkmann wrote:

> On 8/24/05, David A. Black <dblack / wobblini.net> wrote:
>
>> 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. ;-)

They may be the same in other languages.  In Ruby they're related but
different.

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

Yes, by definition, @vars are instance variables.

> Are you saying that attributes are implied by the existence of get and
> perhaps set methods?

There are a couple of ways to look at that.  My reasoning is as
follows:

The utility of method pairs like this:

   def x; @x; end
   def x=(y); @x = y; end

is so great, that the attr_* family of automatic method-writers was
created to make it easy to write them.

The "attr" part of attr_* means attribute -- so presumably those
get/set method pairs give you an attribute.

However, in my opinion, there's no reason to limit the concept of
"attribute" to those that happen to be created with attr_*, or those
that take exactly the same form.  You might have an 'x' attribute, and
then decide you need something more complex:

   def x=(y); @x = y.capitalize; end

I personally don't think that should make it less of an 'attribute'.

One could argue it differently, though.  In a sense, the *only* thing
that makes something an attribute (as opposed to just a method or pair
of methods) is the presence of the attr_* call.  So those shortcut
methods also serve as a kind of documentation of your attributes.

But I think it's a bit harsh, so to speak, to disqualify what might
otherwise be called an attribute because it isn't implemented exactly
the way attr_* implements them.  I don't know that it really makes
much difference, except in documentation (and I know there's been
disucssion in the past, for example, of whether RDoc should view
attributes as a separate construct, or just document them as their
underlying methods).

On the other side of the picture: even if you do choose to refer only
to the attr_*/instance-var-based get/set methods as attributes, it's
still true that instance variables are not constrained by that
definition.  They can be used for many other purposes.

(I'll stop here and maybe someone else will chime in on Structs.)


David

-- 
David A. Black
dblack / wobblini.net