Protected instance variables

M

Mystifier

------=_NextPart_000_0031_01C4FFDF.E9602A40
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hi,

I read somewhere @_ will be used to represent protected instance variables.
What will be the scope of it.


Mystifier

------=_NextPart_000_0031_01C4FFDF.E9602A40--
 
D

David A. Black

Hi --

Hi,

I read somewhere @_ will be used to represent protected instance variables.
What will be the scope of it.

My understanding is that they'll be module/class-scoped:

module M
def a
@_var = 1
end
end

class C
include M
def b
puts @_var
end
end

c = C.new
c.a
c.b # nil

I could be getting it wrong, though.

Also I believe that Matz hasn't decided whether it will be @_var or
@var. If the latter, then @_var would be for non-protected ones. My
hatred of extra punctuation is such that I would rather see them all
behave the same way (whatever it is) and be @var.


David
 
M

Mystifier

By going with C++ convention, currently @vars are protected, new ones will
be private. Right?

Mystifier

-----Original Message-----
From: dblack@wobblini [mailto:dblack@wobblini] On Behalf Of David A. Black
Sent: Friday, January 21, 2005 6:42 PM
To: ruby-talk ML
Subject: Re: Protected instance variables


Hi --

Hi,

I read somewhere @_ will be used to represent protected instance variables.
What will be the scope of it.

My understanding is that they'll be module/class-scoped:

module M
def a
@_var = 1
end
end

class C
include M
def b
puts @_var
end
end

c = C.new
c.a
c.b # nil

I could be getting it wrong, though.

Also I believe that Matz hasn't decided whether it will be @_var or
@var. If the latter, then @_var would be for non-protected ones. My
hatred of extra punctuation is such that I would rather see them all
behave the same way (whatever it is) and be @var.


David
 
D

David A. Black

Hi --

By going with C++ convention, currently @vars are protected, new ones will
be private. Right?

I'm not sure whether it maps onto C++, but I don't think that's a
goal. My understanding was that the problem was accidental
overwriting of instance variables -- for example, if you include a
module but haven't seen the code so you don't know what names the
author used for instance variables. So having them be
[protected,private] (not sure which is the right term) would mean you
could make up your own instance variable names in class or module
scope and not have to worry.


David
 
T

trans.

David wrote:
So having them be [protected,private] (not sure which is
the right term) would mean you could make up your own
instance variable names in class or module scope and not
have to worry.

Hmm... That certainly seems like a good idea. And then you'd use
accessors to get at those on different levels. But attribute names
(i.e. method names for accessing those instance vars) would still come
into conflict, which partially of defeats the purpose. ?
 
D

David A. Black

Hi --

David wrote:
So having them be [protected,private] (not sure which is
the right term) would mean you could make up your own
instance variable names in class or module scope and not
have to worry.

Hmm... That certainly seems like a good idea. And then you'd use
accessors to get at those on different levels.

If you want. Or they may just be used to store state privately.
But attribute names
(i.e. method names for accessing those instance vars) would still come
into conflict, which partially of defeats the purpose. ?

That shouldn't be a problem, because if a module defines an accessor
method, that will be documented and if you're writing a class that
includes that module, you'll know not to override it unless you want
to (as with other methods).


David
 
T

trans.

Okay, that makes sense for public attribute methods. Does it apply as
well to private and protected?

FYI, I'm thinking about @vars always being "local" and no such thing as
@_ non-locals, rather accessors would be needed. It's come up before
but I don't recall anyone laying out the pros and cons really.
 
D

David A. Black

Hi --

Okay, that makes sense for public attribute methods. Does it apply as
well to private and protected?

I would think so. If you're going to do:

class C
include M
def x
# ...
end
end

I think you really have to know whether there's an x method in M, at
any privacy level.


David
 
T

trans.

I see. Thanks. (And a good reason to be sure to document even ones
non-public methods then.)

Actually that helps me understand the fore mentioned alternate concept
of having only pure locals. Right now, we have instance vars that are
accssable from anywhere in the hierarchy. That's like a submethod being
able to access the local vars of its super!

Nonetheless, I recall some were dead set against the idea. Perhaps
unfortuately, I thinks most of us have become quite accustomed to it.
 

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,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top