which means one more set of scoping rules to keep mind? (Probably
not, but even the prospect makes me shudder;-)
Actually, that raises an interesting point.
If we’re (somewhat unfortunately, imo) using ‘_’ to denote ‘localized’,
then should we be using more orthogonally?
@_a - localized instance variable
_a - localized block variable,
_a = 1
10.times {|_a| puts _a}
puts _a # => 1
If we’re going down this route (and I’m wondering if I think we
should), then we can at least make us of the options.
Jim Hranicky, Senior SysAdmin UF/CISE Department |
E314D CSE Building Phone (352) 392-1499 |
jfh@cise.ufl.edu http://www.cise.ufl.edu/~jfh |
“Given a choice between a complex, difficult-to-understand, disconcerting
explanation and a simplistic, comforting one, many prefer simplistic
comfort if it’s remotely plausible, especially if it involves blaming
someone else for their problems.”
– Bob Lewis, Infoworld
Would non-localized block variables:
a) retain their original behavior (that is, they are local if the
variable already exists in the outer block, otherwise they are
non-local), or
b) always be non-local?
I like (b) since it’s cleaner, but (a) because it doesn’t break any
code.
On a side note, I wish there were another symbol that could be used in
place of _. It makes variables look asymmetric (unless you write a).
Paul
···
On Thu, Sep 12, 2002 at 11:17:03PM +0900, Dave Thomas wrote:
Wasn’t there some talk about using symbols? It’s not symmetric, but for
some reason it looks better to me.
a = 1
10.times { |:a| puts a }
puts a # => 1
···
On Fri, Sep 13, 2002 at 11:07:26PM +0900, Paul Brannan wrote:
On Thu, Sep 12, 2002 at 11:17:03PM +0900, Dave Thomas wrote:
_a - localized block variable,
_a = 1
10.times {|_a| puts _a}
puts _a # => 1
Interesting idea.
Would non-localized block variables:
a) retain their original behavior (that is, they are local if the
variable already exists in the outer block, otherwise they are
non-local), or
b) always be non-local?
I like (b) since it’s cleaner, but (a) because it doesn’t break any
code.
On a side note, I wish there were another symbol that could be used in
place of _. It makes variables look asymmetric (unless you write a).
Well, will these localized/private variables make it into the next Ruby
release? Because there were some previous objections regarding the single
leading underscore (“I have used leading underscore in my current instance
variables”), or regarding the symmetry (“It will not look balanced”), will
there be any vote on the format of the localized variables:
@_x
@__x
@_x_
@__x__ (ala Python ?)
(or some other second character indicator, such as @#x and @%x ?)
Or will Matz decide on this?
Regards,
Bill
···
============================================================================
Christian Szegedy szegedy@nospam.or.uni-bonn.de wrote:
@_a - localized instance variable
_a - localized block variable,
This is a very nice and consistent idea. I like this better than anything else:
it is convenient and easy to remeber.
Well, I am proposing 2 alternatives that I think is more consistent with
the rest of Ruby.
The use of the keyword “private”
Any methods defined after this keyword is private. So probably we can
make any new instance variables created after this keyword to be also
private. But mixing the private and non-private variables will be
confusing.
The use of a new symbol.
Ruby is to some extent influenced by Perl, and in Perl “$”, “@”, and
“%” are used to denote the type of variables. Ruby has used “$” and
“@” for special meaning for variables, so this suggests to use “%” prefix
for private variables.
Regards,
Bill
···
============================================================================
Alan Chen alan@digikata.com wrote:
On a side note, I wish there were another symbol that could be used in
place of _. It makes variables look asymmetric (unless you write a).
Paul
Wasn’t there some talk about using symbols? It’s not symmetric, but for
some reason it looks better to me.
Well, will these localized/private variables make it into the next Ruby
release? Because there were some previous objections regarding the single
leading underscore (“I have used leading underscore in my current instance
variables”), or regarding the symmetry (“It will not look balanced”), will
there be any vote on the format of the localized variables:
@_x
@__x
@_x_
@__x__ (ala Python ?)
(or some other second character indicator, such as @#x and @%x ?)
Or will Matz decide on this?
Ultimately Matz will decide on it, whether there’s a vote or not
(That’s part of the “benevolent dictatorship” But still, in terms
of community consensus – has it really come down to wanting extra
punctuation? Isn’t this one of the things (almost) everyone expresses
relief about Ruby not having very much of?
Personally I’d be sad to see any more double punctuation variable
semantics in Ruby (@@cvar being the only one I can think of). It’s
true that declaring things private (rather than tagging them as
private with an underscore) means that one can’t see privacy at a
glance. But that’s already true of private methods – i.e., their
names are not required to be different from names of other methods.
David
···
On Mon, 16 Sep 2002, William Djaja Tjokroaminata wrote:
In message “Re: private variables” on 02/09/16, William Djaja Tjokroaminata billtj@y.glue.umd.edu writes:
Well, will these localized/private variables make it into the next Ruby
release? Because there were some previous objections regarding the single
leading underscore (“I have used leading underscore in my current instance
variables”), or regarding the symmetry (“It will not look balanced”), will
there be any vote on the format of the localized variables:
@_x @__x
@x
@x (ala Python ?)
(or some other second character indicator, such as @#x and @%x ?)
Or will Matz decide on this?
I will. Somewhere around “@_x” and “@x". I don’t care about the
balancing. Those who care may put extra "” after varriable names.
The use of the keyword “private”
Any methods defined after this keyword is private. So probably we can
make any new instance variables created after this keyword to be also
private. But mixing the private and non-private variables will be
confusing.
private variables being differnt from local ones how? further can a
keyword like this work?. other keywords are just shorthands aren’t they?
well, maybe it can.
The use of a new symbol.
Ruby is to some extent influenced by Perl, and in Perl “$”, “@”, and
“%” are used to denote the type of variables. Ruby has used “$” and
“@” for special meaning for variables, so this suggests to use “%” prefix
for private variables.
you see that’s interesting. that’s what i purposed for local variables
to end writer method ambiguities. ie. does x = 1 mean local variable x =
1, or self.x = 1.
-tom
···
On Fri, 2002-09-13 at 13:02, William Djaja Tjokroaminata wrote:
Well, I am proposing 2 alternatives that I think is more consistent with
the rest of Ruby.
The use of the keyword “private”
Any methods defined after this keyword is private. So probably we can
make any new instance variables created after this keyword to be also
private. But mixing the private and non-private variables will be
confusing.
I’d prefer having something explicit like:
private_variables :some, :privvy, :vars, :listed, :here
making @some, @privvy, @vars, @listed and @here private in that class.
And who knows, perhaps there is some magic tricks to be learned if
privateness can be dynamic and delayed. (I.e. define class, methods and
make instances, then pull a class_eval { private_variables :somevar })
The use of a new symbol.
Ruby is to some extent influenced by Perl, and in Perl “$”, “@”, and
“%” are used to denote the type of variables. Ruby has used “$” and
“@” for special meaning for variables, so this suggests to use “%” prefix
for private variables.
IIRC, neither of $ or @ are used as operators, but % is modulus in Ruby.
I can’t think of any case where it would not be backwards compatible,
but I think it looks confusing:
a = b%c # b modulus c
a = b %c # call method b with one argument; private variable c
But then, perhaps we have enough training dealing with this confusion
due to things like a +5 versus a+5…
Now, earlier when the talk of local variables came up, I noticed the £
sign sitting to the left of the $ on my keyboard. The pound sign almost
looks like a L too! (L for local. ) Or maybe even §, which looks like to
S’s (supersecret perhaps). Don’t take these ideas too serious though. I
do not prefer to program in ASCII art.
···
–
([ Kent Dahl ]/)_ ~[ http://www.stud.ntnu.no/~kentda/ ]/~
))_student/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_/ö____/ (_engineering.discipline=Computer::Technology)
Personally I'd be sad to see any more double punctuation variable
semantics in Ruby (@@cvar being the only one I can think of). It's
true that declaring things private (rather than tagging them as
private with an underscore) means that one can't see privacy at a
glance. But that's already true of private methods -- i.e., their
names are not required to be different from names of other methods.
In this case where do you store class local variables (i.e. what you call
private variables :-)) ?
Personally I’d be sad to see any more double punctuation variable
semantics in Ruby (@@cvar being the only one I can think of). It’s
true that declaring things private (rather than tagging them as
private with an underscore) means that one can’t see privacy at a
glance. But that’s already true of private methods – i.e., their
names are not required to be different from names of other methods.
Private methods and class-local instance variables are quite different.
If you use a private method, you get an exception. If you try to use
a class-local instance variable then you just use another variable,
which could lead to obscure bugs.
So I think punctuation (and it should be as short as possible)
is a much better option.
Well, the double-underscore (@__x) variables would be less likely in
Ruby code right now than single (@_x) since Ruby uses doubles for
special constants like FILE, etc so it may yield less conflicts with
existing code bases.
-rich
···
-----Original Message-----
From: Yukihiro Matsumoto [mailto:matz@ruby-lang.org]
Sent: Monday, September 16, 2002 10:46 AM
To: ruby-talk ML
Subject: Re: private variables
Hi,
In message “Re: private variables” > on 02/09/16, William Djaja Tjokroaminata > billtj@y.glue.umd.edu writes:
Well, will these localized/private variables make it into
the next Ruby
release? Because there were some previous objections regarding the
single leading underscore (“I have used leading underscore in my
current instance variables”), or regarding the symmetry (“It
will not
look balanced”), will there be any vote on the format of the
localized
variables:
@_x @__x
@x
@x (ala Python ?)
(or some other second character indicator, such as @#x and @%x ?)
Or will Matz decide on this?
I will. Somewhere around “@_x” and “@x". I don’t care
about the balancing. Those who care may put extra "” after
varriable names.