[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Protected instance variables

Mystifier

1/21/2005 12:08:00 PM

Hi,

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


Mystifier
8 Answers

dblack

1/21/2005 1:12:00 PM

0

Mystifier

1/21/2005 1:34:00 PM

0

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 --

On Fri, 21 Jan 2005, Mystifier wrote:

> 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

--
David A. Black
dblack@wobblini.net




dblack

1/21/2005 1:42:00 PM

0

tsawyer

1/21/2005 2:08:00 PM

0

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. ?

dblack

1/21/2005 2:31:00 PM

0

tsawyer

1/21/2005 5:21:00 PM

0

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.

dblack

1/21/2005 5:41:00 PM

0

tsawyer

1/21/2005 6:04:00 PM

0

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.