Making Instace Variables Private/Local

Hi Matz,

If you are still considering to use ‘@_var’ or ‘@__var’ to create class
private/local variables, are you open to the idea of simply making all
instance variables private? The descendant classes then can access the
instance variables using ‘self.var’, where the method ‘var’ can either be
public or protected.

The reason I ask is because it seems that you are considering things like
‘abc[0]’ which will return the “char” instead of the “fixnum” in 1.9,
which may break a lot of codes. So although currently making all instance
variables private will also break a lot of codes, probably the
“transition” can be handled in the same way as the behavior change of
‘abc[0]’. But first of course I want to know whether you think making all
instance variables private is a good idea in the first place (ignoring
the backward compatibility issue for now), especially from the
object-oriented data hiding point of view, and in particular for doing
large-scale software development.

Regards,

Bill

···

============================================================================
Hi,

In message “Re: Thoughts on Ruby” on 02/11/03, “Gavin Sinclair” gsinclair@soyabean.com.au writes:

(regarding the fact “abc”[0] being fixnum)

It’s probably not intuitive to anybody (except perhaps Matz). However,
I see
no particular justification for it being any other way, and in fact it’s
changing in 1.7 isn’t it? Anyway, my point is: you’ll never forget it
now.
And if you’re like me, you won’t care either.

It will in 1.9 or later.

						matz.

Hi –

If you are still considering to use ‘@_var’ or ‘@__var’ to create class
private/local variables, are you open to the idea of simply making all
instance variables private? The descendant classes then can access the
instance variables using ‘self.var’, where the method ‘var’ can either be
public or protected.

Your second sentence doesn’t necessarily relate to your initial
question :slight_smile: Meaning: whether instance vars are private or not,
there’s no reason to think that every instance var is going to have
one of these:

def var
@var
end

associated with it. All sorts of other permutations are
possible… including no accessor method, or this:

def var
@thing
end

or this:

def var
@var * 2
end

etc.

David

···

On Thu, 7 Nov 2002, William Djaja Tjokroaminata wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi,

···

In message “Making Instace Variables Private/Local” on 02/11/07, William Djaja Tjokroaminata billtj@y.glue.umd.edu writes:

If you are still considering to use ‘@_var’ or ‘@__var’ to create class
private/local variables, are you open to the idea of simply making all
instance variables private? The descendant classes then can access the
instance variables using ‘self.var’, where the method ‘var’ can either be
public or protected.

I am not brave enough to introduce such incompatibility. I guess more
than 60% of Ruby programs will not run after such change.

						matz.

Meaning: whether instance vars are private or not,

Well, the problem is that the use of "private instance variable" is very
bad when someone want to refer to @__var, class local variable seems
better (see [ruby-talk:50212] and [ruby-talk:50242])

Guy Decoux

I think Bill’s point is that it would be better for instance variables
to be hidden behind an interface made up of public or protected methods
so that the implementation of a base class can change without breaking
derived classes. And I have to agree with him.

Cheers,
Nat.

···

On Thu, 2002-11-07 at 15:31, dblack@candle.superlink.net wrote:

Hi –

On Thu, 7 Nov 2002, William Djaja Tjokroaminata wrote:

If you are still considering to use ‘@_var’ or ‘@__var’ to create class
private/local variables, are you open to the idea of simply making all
instance variables private? The descendant classes then can access the
instance variables using ‘self.var’, where the method ‘var’ can either be
public or protected.

Your second sentence doesn’t necessarily relate to your initial
question :slight_smile: Meaning: whether instance vars are private or not,
there’s no reason to think that every instance var is going to have
one of these:

def var
@var
end

associated with it. All sorts of other permutations are
possible… including no accessor method, or this:

def var
@thing
end

or this:

def var
@var * 2
end

etc.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav


Dr. Nathaniel Pryce, Technical Director, B13media Ltd.
Studio 3a, 22-24 Highbury Grove, London N5 2EA, UK
http://www.b13media.com

dblack@candle.superlink.net wrote:

Your second sentence doesn’t necessarily relate to your initial
question :slight_smile: Meaning: whether instance vars are private or not,
there’s no reason to think that every instance var is going to have
one of these:

def var
@var
end

associated with it. All sorts of other permutations are
possible… including no accessor method.

Hi David,

Totally agree. However, the intent of making all instance variables
private is that when a child class writes ‘@var’, it is a variable that
belongs to that specific child class so that a child class will never
accidentally overwrites a variable belonging to its parent. A child is
never supposed to know its parent’s business; a child is given access only
through its parent’s methods (which may or may not represent a
simple/direct access to the parent’s instance variables with similar
names).

In the case that a child really needs access to its parent’s ‘@var’ but
the parent does not provide any method to access it, then the parent class
definition can be reopened to provide ‘def var; @var; end’. So a child
always accesses its parent’s data using ‘self.var’ (or other
‘self.something’) but never ‘@var’.

Regards,

Bill

Hi Matz,

I guess if you really introduce class local variables (@__var) then
probably people can start migrating to using class local variables and
child classes can start accessing parent’s data using methods. But of
course this boils down to your opinion, whether those 60% of Ruby programs
are better off by migrating to internal data/interface separation. (In
other words, whether you decide that it is perfectly fine that a child
class accesses its parent class data directly in terms of ‘@var’ and not
‘self.var’.)

(For me personally, rather than having the not-very-nice-looking @__var
forever, it is probably better to have some transition period, just like
things that are “deprecated”, such as “type”.)

I am also wondering, among those people who wrote the 60% of Ruby
programs, did it ever occur to any of them in the past an idea to provide
data encapsulation/hiding/protection by accessing parent data using an
interface (accessor method) instead of accessing the parent data directly?

Regards,

Bill

···

Yukihiro Matsumoto matz@ruby-lang.org wrote:

Hi,

In message “Making Instace Variables Private/Local” > on 02/11/07, William Djaja Tjokroaminata billtj@y.glue.umd.edu writes:

If you are still considering to use ‘@_var’ or ‘@__var’ to create class
private/local variables, are you open to the idea of simply making all
instance variables private? The descendant classes then can access the
instance variables using ‘self.var’, where the method ‘var’ can either be
public or protected.

I am not brave enough to introduce such incompatibility. I guess more
than 60% of Ruby programs will not run after such change.

  					matz.

Were Ruby to be invented again, would you do it?

Put another way: do you think it is The Right Thing?

IMHO it is closer to it than what we have now.

PS: first post!

···

On Fri, Nov 08, 2002 at 03:14:36AM +0900, Yukihiro Matsumoto wrote:

Hi,

In message “Making Instace Variables Private/Local” > on 02/11/07, William Djaja Tjokroaminata billtj@y.glue.umd.edu writes:

If you are still considering to use ‘@_var’ or ‘@__var’ to create class
private/local variables, are you open to the idea of simply making all
instance variables private? The descendant classes then can access the
instance variables using ‘self.var’, where the method ‘var’ can either be
public or protected.

I am not brave enough to introduce such incompatibility. I guess more
than 60% of Ruby programs will not run after such change.

from my new PC in my new dorm, I mean :slight_smile:

_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

On a normal ascii line, the only safe condition to detect is a ‘BREAK’

  • everything else having been assigned functions by Gnu EMACS.
    – Tarl Neustaedter

Hi Nat,

Thanks a lot for the support and clarification.

I really think that, except the backward compatibility issue, making all
instance variables “class local variables” will make Ruby even more
suitable for large scale software development and not just
“scripting”. (And “big” software is good, because it is where
corporations usually pour most of their money into :slight_smile: )

Does anyone see any disadvantages of doing this, other than:

  1. Backward compatibility issue (which IMO should be handled in the same
    way as the String#[fixnum] behavior change)

  2. Performance (which can be handled cleanly by changing the class local
    variables to C struct’s if performance is really important)

Regards,

Bill

···

Nat Pryce nat.pryce@b13media.com wrote:

I think Bill’s point is that it would be better for instance variables
to be hidden behind an interface made up of public or protected methods
so that the implementation of a base class can change without breaking
derived classes. And I have to agree with him.

Cheers,
Nat.

Were Ruby to be invented again, would you do it?

Put another way: do you think it is The Right Thing?

Hi batsman,

Thank you, thank you. You can say it much better and more to the
point than I did.

PS: first post!

from my new PC in my new dorm, I mean :slight_smile:

Congratulations, batsman! :slight_smile:

Regards,

Bill

···

Mauricio Fern?ndez batsman.geo@yahoo.com wrote:

Bill, you seem to wait about a week between each restart of this topic. Do
you really think that things will change in that short time? I think that to
champion this topic is positive indication that the concept of inheritance in
OOD is still not grasped.

···


“Every real thought on every real subject knocks the
wind out of somebody or other.”
-Oliver Wendell Holmes, Sr.

Hi,

···

In message “Re: Making Instace Variables Private/Local” on 02/11/08, Mauricio Fernández batsman.geo@yahoo.com writes:

Were Ruby to be invented again, would you do it?

Class local only? No, I don’t think so.

						matz.

(In
other words, whether you decide that it is perfectly fine that a child
class accesses its parent class data directly in terms of ‘@var’ and not
‘self.var’.)

Bill

The only other OO (rather, OI - Object Influenced) languages I know are C++ and
Java. In both of these, derived classes have access to parent classes’ public
(obviously) and protected instance variables.

It seems that the Ruby model is that all instance variables are a mix:

  • protected, because children can access them; and
  • private, because other objects of the same class can’t access them

If C++ and Java allow derived classes to access parent’s instance variables,
then why not Ruby? Granted, at least in these languages, instance variables
can be made genuinely private, but I don’t think it would make sense for Ruby
to make all instance variables private.

In my (limited) experience, there is less need for inheritance in Ruby than in
C++/Java, because dynamic binding takes place all the time, and because classes
can be modified. This is great, because in Ruby you only inherit when it
genuinely makes sense, rather than as a convenience for modifying behaviour. A
corollary of this is that you should be able to manage your inheritence trees
sufficiently to avoid trampling on parent’s data. Just my 2c.

Gavin

···

From: “William Djaja Tjokroaminata” billtj@y.glue.umd.edu [snipped]

Let’s see: Dave Thomas gave a presentation on a 26kloc production
business system he wrote over the summer in Ruby. I have a project
with about 10kloc’s of Ruby controlling a distributed agent society
of 40+ machines in a highly dynamic environment…being used every
day. I never had a need with what you are asking for, and obviously
Dave didn’t.

I think the main barrier to writing production systems in Ruby is
people talking about why it cannot be done and not spending the
time doing it.

-Rich

···

-----Original Message-----
From: Gavin Sinclair [mailto:gsinclair@soyabean.com.au]
Sent: Thursday, November 07, 2002 6:30 PM
To: ruby-talk ML
Subject: Re: Making Instace Variables Private/Local

From: “William Djaja Tjokroaminata” billtj@y.glue.umd.edu [snipped]

(In
other words, whether you decide that it is perfectly fine
that a child
class accesses its parent class data directly in terms of
@var’ and
not
‘self.var’.)

Bill

The only other OO (rather, OI - Object Influenced) languages
I know are C++ and Java. In both of these, derived classes
have access to parent classes’ public
(obviously) and protected instance variables.

It seems that the Ruby model is that all instance variables are a mix:

  • protected, because children can access them; and
  • private, because other objects of the same class can’t
    access them

If C++ and Java allow derived classes to access parent’s
instance variables, then why not Ruby? Granted, at least in
these languages, instance variables can be made genuinely
private, but I don’t think it would make sense for Ruby to
make all instance variables private.

In my (limited) experience, there is less need for
inheritance in Ruby than in
C++/Java, because dynamic binding takes place all the time,
and because
C++classes
can be modified. This is great, because in Ruby you only
inherit when it genuinely makes sense, rather than as a
convenience for modifying behaviour. A corollary of this is
that you should be able to manage your inheritence trees
sufficiently to avoid trampling on parent’s data. Just my 2c.

Gavin

This answer surprises me quite a lot, for the only tangible reason I can
think of is the speed issue. I really felt it was The Right Thing.

But if it’s a matter of ‘style’, I’ll just take it as it is, as I have
already stated I will trust you :slight_smile:

···

On Fri, Nov 08, 2002 at 07:28:44AM +0900, Yukihiro Matsumoto wrote:

Hi,

In message “Re: Making Instace Variables Private/Local” > on 02/11/08, Mauricio Fernández batsman.geo@yahoo.com writes:

Were Ruby to be invented again, would you do it?

Class local only? No, I don’t think so.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

“You, sir, are nothing but a pathetically lame salesdroid!
I fart in your general direction!”
– Randseed on #Linux

Hi Albert,

There is nothing magic with “a week”; I am tied with other things beside
Ruby.

Regards,

Bill

···

Albert Wagner alwagner@tcac.net wrote:

Bill, you seem to wait about a week between each restart of this topic. Do
you really think that things will change in that short time?

Hi Gavin,

But remember, C++ and Java are statically typed languages. I can just
check the one and only one place of variable declarations and I know all
the instance variables. Not so with Ruby; it is fundamentally difficult
to know whether I accidentaly overwrite someone else’s variable with the
same name; only bugs will reveal that… :slight_smile:

Regards,

Bill

···

Gavin Sinclair gsinclair@soyabean.com.au wrote:

If C++ and Java allow derived classes to access parent’s instance variables,
then why not Ruby? Granted, at least in these languages, instance variables
can be made genuinely private, but I don’t think it would make sense for Ruby
to make all instance variables private.

Hi Matz,

Do you have any technical reason for this? (I am trying to learn OO from
you.) Or is it just a dogma or style?

Regards,

Bill

···

Yukihiro Matsumoto matz@ruby-lang.org wrote:

Hi,

Class local only? No, I don’t think so.

  					matz.

Well, it is not about not spending the time doing it; I want to do it, but
to do it in the right way. Really, for a one-person project, if he/she
overwrite his/her own variable of the same name, it is his/her own
problem. But in multi-person project, with a stratagey of, for example,
one person is responsible for one class in a inheritance tree, the
“protected” instance variables can only cause bugs. (This problem IMO is
the very reason Matz was considering the creation of class local
variables.)

If I am the project manager, I will enforce this to my subordinates: when
the class local variables are available, then everyone will use only class
local (private) variables, and child classes will access the parent’s data
using methods, never the variables directly. In this way there is no risk
of bugs because someone by accident creates an identical variable
name. Also, we can change the implementation of a parent class by
changing the instance variables (but not the methods) without breaking any
child classes.

However, if a rule is only a rule specified by people, and not enforced by
the language itself, then it is not good. People can still abuse it
(either intentionally or accidentally). An example is of course C++.

I really want to sell Ruby to the corporate world, but I do not like to
follow the Perl path, but instead I prefer the Java path. I want Matz’s
picture to be on the cover of Time magazine and not just Linux Journal
magazine (such as Larry Wall).

Regards,

Bill

···

Rich Kilmer rich@infoether.com wrote:

Let’s see: Dave Thomas gave a presentation on a 26kloc production
business system he wrote over the summer in Ruby. I have a project
with about 10kloc’s of Ruby controlling a distributed agent society
of 40+ machines in a highly dynamic environment…being used every
day. I never had a need with what you are asking for, and obviously
Dave didn’t.

I think the main barrier to writing production systems in Ruby is
people talking about why it cannot be done and not spending the
time doing it.

-Rich

Hi,

···

In message “Re: Making Instace Variables Private/Local” on 02/11/09, William Djaja Tjokroaminata billtj@z.glue.umd.edu writes:

Do you have any technical reason for this? (I am trying to learn OO from
you.) Or is it just a dogma or style?

Actually inheritance is a very heavy relationship between classes.
Once you’ve decided to inherit from other class, you have to know
inside of superclass anyway.

						matz.