Private variables

“Yukihiro Matsumoto” matz@ruby-lang.org wrote in message
news:1032187553.528959.21468.nullmailer@picachu.netlab.jp…

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?

I will. Somewhere around “@_x” and “@x". I don’t care about the
balancing. Those who care may put extra "
” after varriable names.

or being my repetitive self

  _@x

This probably breaks no (non-obfuscating) existing
code and is quite distinct from regular instance (and
local) variables.

/Christoph

Ps. I am very curious about your opinion on Dave’s

_block_local_variable_idea

(Sorry Dave, hopefully you won’t be haunted by ``the
ghosts that you called’';-).

···

In message “Re: private variables” > on 02/09/16, William Djaja Tjokroaminata billtj@y.glue.umd.edu writes:

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:

No, no see [ruby-talk:12555]

  {
    e = 1 # e may or may not be local to the block
    f := 2 # f is always local to this block
  }

Guy Decoux

Hi,

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.

I haven’t really read the parser yet, but I imagine that after the
detection of keyword “private”, the parser/interpreter will call
“rb_define_private_method()” instead of “rb_define_method()”. Therefore,
in a similar way, after the detection of the keyword “private”, any newly
detected “@var = …” will not call “rb_iv_set()”, but something like
“rb_private_iv_set()” which just simply writes the data in a separate
hash. Now, the “… = @var” case is a little bit more tricky, but I think
it should invoke “rb_private_iv_get()” before searching using
“rb_iv_get()”.

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.

I must be missing something. Why can “x = 1” imply “self.x = 1”?

Regards,

Bill

···

Tom Sawyer transami@transami.net wrote:

Hi –

  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…

Here’s an non-backwards compatible case:

%q__ = “some private thing”
a = %q__

What’s the value of a at this point?

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:

I was musing before on ‘#’, which has a secretive/private connotation,
as a variable prefix :slight_smile: Thinking about these things makes one
increasingly grateful for the non-punctuation-riddenness of Ruby.

David

···

On Sat, 14 Sep 2002, Kent Dahl 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

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

I am not an expert in Ruby parser/interpreter at all (let alone computer
science), but to me this seems perfectly doable. I.e., after the
detection of keyword “private_variables”, all public variables in the list
are moved from the current (public) instance variable hash to the private
instance variable hash. And then writing and reading the instance
variables are like in my other post.

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…

Well, “%” is also used in Perl as a modulus operator, and its use with
variables is fine there. Like you have written above, we can make the
rule. But probably people should write code in an explict way…

Regards,

Bill

···

Kent Dahl kentda@stud.ntnu.no wrote:

Hi –

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

Whoops, I have reintroduced deprecated terminology into the discussion
:slight_smile: And I probably can’t answer your question very cogently. But I
guess I’d store them wherever they’d be stored if they were named
@_var, only I wouldn’t name them @_var.

In the certainty that this can’t be a satisfactory answer, back to
you… :slight_smile:

David

···

On Mon, 16 Sep 2002, ts 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

If you don’t like underscores (because it could break
existing code), how about using ^x and @^x for (class/block)
local variables?

I know, this denotes xor also, but we have a lot of
binary operators which have unary meanings (e.g.: -,*,&,%),
we just would have one more…

But all in all, underscore looks nicer and more familiar
for variable names. (Though, variable_name looks very
ugly.)

Best Regards, Christian

If you try to use a class-local instance variable and it has not yet
been initialized, I would expect to see a warning when you run with ruby
-w, just like you get now with regular instance variables.

Paul

···

On Mon, Sep 16, 2002 at 11:50:54PM +0900, Christian Szegedy wrote:

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.

Hi –

···

On Tue, 17 Sep 2002, Christoph wrote:

or being my repetitive self

  _@x

This probably breaks no (non-obfuscating) existing
code and is quite distinct from regular instance (and
local) variables.

This also makes sense to me because it reads from left to
right: private (_) instance variable (@x). @_x looks
like “instance va… oh, did I forget to metion it was
private? …riable”.

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

Hi,

···

In message “Re: private variables” on 02/09/17, “Christoph” chr_news@gmx.net writes:

I will. Somewhere around “@_x” and “@x". I don’t care about the
balancing. Those who care may put extra "
” after varriable names.

or being my repetitive self

 _@x

This will not break anything, but too ugly.

you don’t have to tell me “@” alone is ugly. :wink:

						matz.

Christoph wrote:

or being my repetitive self

  _@x

This probably breaks no (non-obfuscating) existing
code and is quite distinct from regular instance (and
local) variables.

What, you mean we can't use a single _ as a method name now?

def _(var)
puts “Var is #{var}”
end

@x = nil
_@x = 5 #=> Var is 5
puts @x #=> 5

Why this works at all beats me :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)

Sat, 14 Sep 2002 05:42:44 +0900, William Djaja Tjokroaminata billtj@z.glue.umd.edu pisze:

Well, “%” is also used in Perl as a modulus operator, and its use
with variables is fine there.

Well, the meaning of ‘foo % bar’ depends on whether foo has been
defined as ‘sub foo’ or ‘sub foo()’ :slight_smile:

···


__("< Marcin Kowalczyk
__/ qrczak@knm.org.pl
^^ http://qrnik.knm.org.pl/~qrczak/

I haven’t really read the parser yet, but I imagine that after the
detection of keyword “private”, the parser/interpreter will call
“rb_define_private_method()” instead of “rb_define_method()”. Therefore,
in a similar way, after the detection of the keyword “private”, any newly
detected “@var = …” will not call “rb_iv_set()”, but something like
“rb_private_iv_set()” which just simply writes the data in a separate
hash. Now, the “… = @var” case is a little bit more tricky, but I think
it should invoke “rb_private_iv_get()” before searching using
“rb_iv_get()”.

i see. thanks.

so were talking about instance variables being private? ugh, i’m
confused (sorry i didn’t catch the whole of this thread) arn’t instance
variable always private? you can only access them through accessor
methods. i’m missing somthing here.

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.

I must be missing something. Why can “x = 1” imply “self.x = 1”?

no it can’t which is exaclty what i mean becase any other method will
imply a self, EXCEPT the writer accessor method, unless of course the
local variable is define first. ex-

class It

attr_accessor :x

def seehere
x # same as self.x (self is implied)
x = 10 # not the same as self.x=
x # no longer the same as self.x
end

end

···

On Fri, 2002-09-13 at 14:22, William Djaja Tjokroaminata wrote:


tom sawyer, aka transami
transami@transami.net

Hi,

I think you are right that “%X” sometimes is already special in Ruby (such
as %q, %Q, %w, %r, and %x). However, the use of “#” is really scary,
because it will make most, if not all, comments break down.

Hmmm, time to search other Perl symbols… how about "", anyone? Has
this symbol been used in Ruby?

Regards,

Bill

···

===========================================================================
dblack@candle.superlink.net wrote:

Here’s an non-backwards compatible case:

%q__ = “some private thing”
a = %q__

What’s the value of a at this point?

I was musing before on ‘#’, which has a secretive/private connotation,
as a variable prefix :slight_smile: Thinking about these things makes one
increasingly grateful for the non-punctuation-riddenness of Ruby.

William Djaja Tjokroaminata wrote:

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.

I think this is a matter of taste, but for me a naming convention
seems to be better, since it would be simpler to tell the difference
for a particular variable.

I am not an expert in Ruby parser/interpreter at all (let alone computer
science), but to me this seems perfectly doable. I.e., after the
detection of keyword “private_variables”, all public variables in the list
are moved from the current (public) instance variable hash to the private
instance variable hash. And then writing and reading the instance
variables are like in my other post.

It would mean a slight slowdown for the access of all instance variables,
since you would need two hash lookups instead of one. If you use the naming
convention, the parser would be able to generate a node for the correct
hash lookup: you need only one (like now).

Best Regards, Christian

···

Kent Dahl kentda@stud.ntnu.no wrote:

David:

Did you ever find out how far from the SEA airport
to the conference center and hotel?

Thanks

···

--
Jim Freeze
----------
Programming Ruby
def initialize; fun; end
A language with class

Whoops, I have reintroduced deprecated terminology into the discussion
:slight_smile: And I probably can't answer your question very cogently. But I
guess I'd store them wherever they'd be stored if they were named
@_var, only I wouldn't name them @_var.

and if someone define an instance variable @_var, this instance variable
become a class local variable automatically (because it's stored with
class local variable), no ?

Guy Decoux

Hi,

Can’t we just add something like “local_iv_table” in addition to
“generic_iv_table” in variable.c? All local variables will use something
like “rb_local_ivar_set()” and “rb_local_ivar_get()” which refers to
local_iv_table.

Regards,

Bill

···

=============================================================================
dblack@candle.superlink.net wrote:

On Mon, 16 Sep 2002, ts wrote:

In this case where do you store class local variables (i.e. what you call
private variables :-)) ?

Whoops, I have reintroduced deprecated terminology into the discussion
:slight_smile: And I probably can’t answer your question very cogently. But I
guess I’d store them wherever they’d be stored if they were named
@_var, only I wouldn’t name them @_var.

In the certainty that this can’t be a satisfactory answer, back to
you… :slight_smile:

dblack wrote

In this case where do you store class local variables (i.e. what you call
private variables :-)) ?

Whoops, I have reintroduced deprecated terminology into the discussion
:slight_smile: And I probably can’t answer your question very cogently. But I

Regard yourself as a first time offender - third strike and you will be
condemned to a life behind C+±bars (foos whatever;-).

/Christoph

Are we dead set against “attr_private” ?

class Foo
attr_private :bar
end

?

I may have missed the part in the conversation where “@__foo” was decided
to be more desirable…this has been a long thread :->

···

On Tue, 17 Sep 2002 01:21:39 +0900 matz@ruby-lang.org (Yukihiro Matsumoto) wrote:

Hi,

In message “Re: private variables” > on 02/09/17, “Christoph” chr_news@gmx.net writes:

I will. Somewhere around “@_x” and “@x". I don’t care about the
balancing. Those who care may put extra "
” after varriable names.

or being my repetitive self

 _@x

This will not break anything, but too ugly.

you don’t have to tell me “@” alone is ugly. :wink:


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