Private variables

How about an attr_private directive?

class Foo
attr_accessor :a, :b;
attr_private :c
end

class Bar < Foo
attr_accessor :c
end

x = Bar.new
p x.c # Actually using Bar::c, not Foo::c

Seems syntactically cleaner to me. Of course, throwing back to the local block
variable thing, how about

  • default to all local, indicate lookups in previous scopes with ::

    foo.each { |x,::y|

    a = x 		# local a modified, value of local x
    ::b = y		# global b now value of global y
    

    }
    ?

···

On Thu, 12 Sep 2002 23:17:03 +0900 Dave Thomas Dave@PragmaticProgrammer.com wrote:

“Christoph” chr_news@gmx.net writes:

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

  _a - localized block variable,
       _a = 1
       10.times {|_a| puts _a}
       puts _a # => 1

This is easy

pigeon% ruby -e '_a = 12; 3.times {|_a| puts _a}; puts _a'
0
1
2
12
pigeon%

:slight_smile:

Guy Decoux

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

Paul

···

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

Dave Thomas wrote:

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,

This is a very nice and consistent idea. I like this better than anything else:
it is convenient and easy to remeber.

I really like this.

Best Regards, Christian

- this also makes Guy's ``attr_reader problem'' obsolete?

attr_reader (or attr_accessor) is not a problem : you can always check if
the name of the attribute is private or not. For example you have in
eval.c

rb_attr(klass, id, read, write, ex)
    VALUE klass;
    ID id;
    int read, write, ex;
{
    const char *name;
    char *buf;
    ID attriv;
    int noex;
[...]

    name = rb_id2name(id);
[...]

Just add

    if (rb_is_private_id(id)) {
       rb_raise(rb_eArgError, "private variable ... ");
    }
    name = rb_id2name(id);

Guy Decoux

“ts” wrote in

pigeon% ruby -e ‘_a = 12; 3.times {|_a| puts _a}; puts _a’
0
1
2
12
pigeon%

:slight_smile:

Neato! – Actually I sort of like Dave’s idea - it is probably
better then the 1001 incarnations of

{<a; b | local z
i_am_block_local := …
etc
}

/Christoph

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

Paul


Alan Chen
Digikata LLC
http://digikata.com

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.

I really like this.

Best Regards, Christian

is that colon there on purpose?

if so, it woulld also get rid of the ambiguity between local variable
assignment and calling standard writter methods.

just fyi

···

On Thu, 2002-09-12 at 19:21, Christoph wrote:

Neato! – Actually I sort of like Dave’s idea - it is probably
better then the 1001 incarnations of

{<a; b | local z
i_am_block_local := …
etc
}


tom sawyer, aka transami
transami@transami.net

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

I might note that any variable name which isn’t palindromic is
similarly asymmetric, no?

Or do you actually only use palindromes?

···

=====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell


Do you Yahoo!?
Yahoo! News - Today’s headlines

Well, I am proposing 2 alternatives that I think is more consistent with
the rest of Ruby.

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

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

a = 1

10.times { |:a| puts a }

puts a # => 1


Alan Chen
Digikata LLC
http://digikata.com

Hi –

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 :slight_smile:
(That’s part of the “benevolent dictatorship” :slight_smile: 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:


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

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.

						matz.

uh-oh, looks like Ruby is headed the way of perl. can anyone say,
rubyisms?

···

On Mon, 2002-09-16 at 08:10, William Djaja Tjokroaminata wrote:

@_x
@__x
@_x_
@__x__ (ala Python ?)
(or some other second character indicator, such as @#x and @%x ?)


tom sawyer, aka transami
transami@transami.net

Hi –

···

On Fri, 13 Sep 2002, Tom Sawyer wrote:

On Thu, 2002-09-12 at 19:21, Christoph wrote:

Neato! – Actually I sort of like Dave’s idea - it is probably
better then the 1001 incarnations of

{<a; b | local z
i_am_block_local := …
etc
}

is that colon there on purpose?

if so, it woulld also get rid of the ambiguity between local variable
assignment and calling standard writter methods.

I think Christoph’s point (with the colon and the rest of the
punctuation) was to illustrate the visual and stylistic dangers
of such things :slight_smile:

David


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

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

  1. 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:


tom sawyer, aka transami
transami@transami.net

William Djaja Tjokroaminata wrote:

Well, I am proposing 2 alternatives that I think is more consistent with
the rest of Ruby.

  1. 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 })

  1. 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. :slight_smile:

···


([ 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 :-)) ?

Guy Decoux

dblack@candle.superlink.net wrote:

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.

Best Regards, Christian

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.

  					matz.