Public/protected/private syntax

I tend to find the public/protected/private keywords in Ruby a little odd.
They don’t seem to fit the Ruby syntax. What about the following syntax (for
Ruby 3.0 :)? The scope of the method would be known by the first caracters of
its name, the same way as the scope of a variable is determined by $, @, @@
or nothing. So:

class Test
def method1; end # public method
def _method2; end # protected method
def __method3; end # private method
end

Just throwing an idea,
Guillaume

Guillaume Marcais wrote:

I tend to find the public/protected/private keywords in Ruby a little odd.
They don’t seem to fit the Ruby syntax. What about the following syntax (for
Ruby 3.0 :)? The scope of the method would be known by the first caracters of
its name, the same way as the scope of a variable is determined by $, @, @@
or nothing. So:

class Test
def method1; end # public method
def _method2; end # protected method
def __method3; end # private method
end

But, in ruby, many methods that you can use without a receiver are in
fact private methods of all objects. For example, #puts, #gsub,
#require, #fork, … (most if not all of these are actually inherited
from Kernel).

That means you would have to type

__require ‘mylib’

all the time.

Hi –

···

On Fri, 16 May 2003, Guillaume Marcais wrote:

I tend to find the public/protected/private keywords in Ruby a
little odd. They don’t seem to fit the Ruby syntax.

They’re actually methods (not keywords) with standard method syntax.
Can you specify what you don’t like? We may be able to talk you
through it :slight_smile:

David


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

“Guillaume Marcais” guslist@free.fr schrieb im Newsbeitrag
news:B0000560361@smtp.rapiddsl.net

I tend to find the public/protected/private keywords in Ruby a little
odd.
They don’t seem to fit the Ruby syntax.

Can you explain that? What do you mean by “don’t fit”?

What about the following syntax (for
Ruby 3.0 :)? The scope of the method would be known by the first
caracters of
its name, the same way as the scope of a variable is determined by $, @,
@@
or nothing. So:

class Test
def method1; end # public method
def _method2; end # protected method
def __method3; end # private method
end

Err, this is merely a convention for identifiers but a change in syntax.
Apart from the fact that it might break existing code personally I prefer
the more elaborate version with explicitely written access modifiers.
Also I’ve been told that it’s not too good an idea to attach semantic to
certain identifiers or identifier formatting.

Regards

robert

Agreed that is not convenient. Didn’t think of that one.

Guillaume.

···

On Thursday 15 May 2003 05:26 pm, you wrote:

Guillaume Marcais wrote:

I tend to find the public/protected/private keywords in Ruby a little
odd. They don’t seem to fit the Ruby syntax. What about the following
syntax (for Ruby 3.0 :)? The scope of the method would be known by the
first caracters of its name, the same way as the scope of a variable is
determined by $, @, @@ or nothing. So:

class Test
def method1; end # public method
def _method2; end # protected method
def __method3; end # private method
end

But, in ruby, many methods that you can use without a receiver are in
fact private methods of all objects. For example, #puts, #gsub,
#require, #fork, … (most if not all of these are actually inherited
from Kernel).

That means you would have to type

__require ‘mylib’

all the time.

Can you explain that? What do you mean by “don’t fit”?

I find it more convenient when the scope of the method is directly readable
from the declaration of the method. In Java, public/protected/private are
qualifiers:

private int dontcallme();

In ruby, I need a second line to specify the scope of the method. Or the
default scope was change before the method declaration. In both cases I have
to look around the code to find what the scope of the method is.

With variables, the name only gives me all that information. It took me a
little while to get used to it (I was more used to the $ used the shell way,
to retrieve the value of the variable), I finally find it very convenient. I
was wondering if a similar concept could work with methods.

Nevertheless, I do realize that what I proposed would break many existing
code. I just threw the idea to see what people think of it, if there is a way
to make it fly. If not, I can live very well with the existing mechanism :slight_smile:

I also notice that the ProgrammingRuby documentation, for example, doesn’t
specify which methods are plublic, protected or private. Is the scope of
methods not considered important in Ruby? On the other hand, changing the
documentation doesn’t need such a dramatich syntax change as the one I
proposed :slight_smile:

Guillaume.

···

On Friday 16 May 2003 03:38 am, you wrote:

What about the following syntax (for
Ruby 3.0 :)? The scope of the method would be known by the first

caracters of

its name, the same way as the scope of a variable is determined by $, @,

@@

or nothing. So:

class Test
def method1; end # public method
def _method2; end # protected method
def __method3; end # private method
end

Err, this is merely a convention for identifiers but a change in syntax.
Apart from the fact that it might break existing code personally I prefer
the more elaborate version with explicitely written access modifiers.
Also I’ve been told that it’s not too good an idea to attach semantic to
certain identifiers or identifier formatting.

Regards

robert

Can you explain that? What do you mean by “don’t fit”?

I find it more convenient when the scope of the method is directly readable
from the declaration of the method. In Java, public/protected/private are
qualifiers:

private int dontcallme();

In ruby, I need a second line to specify the scope of the method. Or the
default scope was change before the method declaration. In both cases I have
to look around the code to find what the scope of the method is.

With variables, the name only gives me all that information. It took me a
little while to get used to it (I was more used to the $ used the shell way,
to retrieve the value of the variable), I finally find it very convenient. I
was wondering if a similar concept could work with methods.

IMHO, it makes sense to demand such a prefix to save typing (although I
don’t agree with that and certainly not want that), but why should I
have to specify the “protection prefix” on method call? Do I really care
about the status of the method? The call either succeeds or fails, but
having a prefix is worthless.

As for having to write private :method instead of “private def method”
or such: you can always group private things at the end and put a single
“private clause” before. This saves even more typing :slight_smile:

Nevertheless, I do realize that what I proposed would break many existing
code. I just threw the idea to see what people think of it, if there is a way
to make it fly. If not, I can live very well with the existing mechanism :slight_smile:

I also notice that the ProgrammingRuby documentation, for example, doesn’t
specify which methods are plublic, protected or private. Is the scope of
methods not considered important in Ruby? On the other hand, changing the
documentation doesn’t need such a dramatich syntax change as the one I
proposed :slight_smile:

Why would private methods be documented? They belong to the implementation
of a class, not to its interface.

As for protected, you’re only going to need it if you intend your class
to be inherited and want to expose some part of the implementation to
subclasses, which does certainly not happen in the case of the core
classes.

Inheritance is not really used a lot in Ruby, because
“it’s at best implementation sharing” (matz dixit)

···

On Fri, May 16, 2003 at 11:33:21PM +0900, Guillaume Marcais wrote:

On Friday 16 May 2003 03:38 am, you wrote:


_ _

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

How should I know if it works? That’s what beta testers are for. I
only coded it.
– Attributed to Linus Torvalds, somewhere in a posting

“Guillaume Marcais” guillaume.marcais@free.fr schrieb im Newsbeitrag
news:B0000560431@smtp.rapiddsl.net

Can you explain that? What do you mean by “don’t fit”?

I find it more convenient when the scope of the method is directly
readable
from the declaration of the method. In Java, public/protected/private
are
qualifiers:

private int dontcallme();

Yes, I like that approach, too.

In ruby, I need a second line to specify the scope of the method. Or the
default scope was change before the method declaration. In both cases I
have
to look around the code to find what the scope of the method is.

Well, nobody stops you using a “private” et al like in Java directly
before each method. So in effect you want to force others to use this
pattern (either by enforcing a modifier or by using underscores). I doubt
you will convince a lot of people though.

With variables, the name only gives me all that information. It took me
a
little while to get used to it (I was more used to the $ used the shell
way,
to retrieve the value of the variable), I finally find it very
convenient. I
was wondering if a similar concept could work with methods.

Nevertheless, I do realize that what I proposed would break many
existing
code. I just threw the idea to see what people think of it, if there is
a way
to make it fly. If not, I can live very well with the existing mechanism
:slight_smile:

Fine. :slight_smile:

I also notice that the ProgrammingRuby documentation, for example,
doesn’t
specify which methods are plublic, protected or private. Is the scope of
methods not considered important in Ruby? On the other hand, changing
the
documentation doesn’t need such a dramatich syntax change as the one I
proposed :slight_smile:

:-))

robert
···

On Friday 16 May 2003 03:38 am, you wrote:

Not really. Sure, you can do:

class Foo
def meth1 …
def meth2 …
def meth3 …
def meth4 …
def meth5 …
def meth6 …

private   :meth3, :meth4
protected :meth5, :meth6

end

Or you can do (C++ style):

class Foo
def meth1 …
def meth2 …

private
def meth3 …
def meth4 …

protected
def meth5 …
def meth6 …
end

-austin
– Austin Ziegler, austin@halostatue.ca on 2003.05.16 at 13:54:23

···

On Fri, 16 May 2003 23:33:21 +0900, Guillaume Marcais wrote:

On Friday 16 May 2003 03:38 am, you wrote:

Can you explain that? What do you mean by “don’t fit”?
I find it more convenient when the scope of the method is directly
readable from the declaration of the method. In Java,
public/protected/private are qualifiers:

private int dontcallme();

In ruby, I need a second line to specify the scope of the method.
Or the default scope was change before the method declaration. In
both cases I have to look around the code to find what the scope
of the method is.

Why would private methods be documented? They belong to the implementation
of a class, not to its interface.

No quite true. You can call all the private method of the Kernel module,
can’t you? A private method can be call as long as there is no receiver
specified.

The proposed syntax would apply to module too (cheering and applause :).

···

On Friday 16 May 2003 11:53 am, you wrote:

As for protected, you’re only going to need it if you intend your class
to be inherited and want to expose some part of the implementation to
subclasses, which does certainly not happen in the case of the core
classes.

Inheritance is not really used a lot in Ruby, because
“it’s at best implementation sharing” (matz dixit)

class Foo
def meth1 …
def meth2 …
def meth3 …
def meth4 …
def meth5 …
def meth6 …

private   :meth3, :meth4
protected :meth5, :meth6

end

As a readability thing, I would prefer to declare the visibility of
the methods up the top of the class (i.e. before defining them). I
like code-as-documentation.

Anyway, that requires manual synchronisation, so I just use RDoc to
tell me what methods a class has!

Gavin

“Austin Ziegler” austin@halostatue.ca schrieb im Newsbeitrag
news:2003516135649.886104@PADD…

Can you explain that? What do you mean by “don’t fit”?
I find it more convenient when the scope of the method is directly
readable from the declaration of the method. In Java,
public/protected/private are qualifiers:

private int dontcallme();

In ruby, I need a second line to specify the scope of the method.
Or the default scope was change before the method declaration. In
both cases I have to look around the code to find what the scope
of the method is.

Not really. Sure, you can do:

class Foo
def meth1 …
def meth2 …
def meth3 …
def meth4 …
def meth5 …
def meth6 …

private   :meth3, :meth4
protected :meth5, :meth6

end

Or you can do (C++ style):

class Foo
def meth1 …
def meth2 …

private
def meth3 …
def meth4 …

protected
def meth5 …
def meth6 …
end

And even (Java style):

class Foo
public def meth1 …
public def meth2 …
private def meth3 …
private def meth4 …
protected def meth5 …
protected def meth6 …
end

:slight_smile:

robert
···

On Fri, 16 May 2003 23:33:21 +0900, Guillaume Marcais wrote:

On Friday 16 May 2003 03:38 am, you wrote:

Why would private methods be documented? They belong to the implementation
of a class, not to its interface.

No quite true. You can call all the private method of the Kernel module,
can’t you? A private method can be call as long as there is no receiver
specified.

Duh!

The proposed syntax would apply to module too (cheering and applause :).

What’s the latest proposal?

private def method ???

···

On Sat, May 17, 2003 at 04:29:16AM +0900, Guillaume Marcais wrote:

On Friday 16 May 2003 11:53 am, you wrote:


_ _

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

How do you power off this machine?
– Linus, when upgrading linux.cs.helsinki.fi, and after using the machine for several months

Hi –

···

On Mon, 19 May 2003, Gavin Sinclair wrote:

class Foo
def meth1 …
def meth2 …
def meth3 …
def meth4 …
def meth5 …
def meth6 …

private   :meth3, :meth4
protected :meth5, :meth6

end

As a readability thing, I would prefer to declare the visibility of
the methods up the top of the class (i.e. before defining them). I
like code-as-documentation.

However…

irb(main):001:0> class A; private :x; def x; end; end
NameError: undefined method x' for class A’
from (irb):1:in `private’
from (irb):1

David


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

And even (Java style):

class Foo
public def meth1 …
public def meth2 …
private def meth3 …
private def meth4 …
protected def meth5 …
protected def meth6 …
end

:slight_smile:

???

class Foo
private def meth1
end
end
SyntaxError: compile error
(irb):3: void value expression
from (irb):4

irb’s output matches my expectations :slight_smile:

···

On Tue, May 20, 2003 at 01:04:36AM +0900, Robert Klemme wrote:


_ _

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

How should I know if it works? That’s what beta testers are for. I
only coded it.
– Attributed to Linus Torvalds, somewhere in a posting

“Mauricio Fernández” batsman.geo@yahoo.com schrieb im Newsbeitrag
news:20030519162336.GB9282@student.ei.uni-stuttgart.de

···

On Tue, May 20, 2003 at 01:04:36AM +0900, Robert Klemme wrote:

And even (Java style):

class Foo
public def meth1 …
public def meth2 …
private def meth3 …
private def meth4 …
protected def meth5 …
protected def meth6 …
end

:slight_smile:

???

class Foo
private def meth1
end
end
SyntaxError: compile error
(irb):3: void value expression
from (irb):4

irb’s output matches my expectations :slight_smile:

Ooops! Should’ve checked. I thougtht this was legal. Darn… Sorry for
the confusion.

robert

Hi,

···

In message “Re: public/protected/private syntax” on 03/05/20, “Robert Klemme” bob.news@gmx.net writes:

Ooops! Should’ve checked. I thougtht this was legal. Darn… Sorry for
the confusion.

This is once proposed by Shugo Maeda long ago, and suspended for
various reason, mostly reluctance to make “def” return symbols.

						matz.

Yukihiro Matsumoto wrote:

Hi,

Ooops! Should’ve checked. I thougtht this was legal. Darn… Sorry for
the confusion.

This is once proposed by Shugo Maeda long ago, and suspended for
various reason, mostly reluctance to make “def” return symbols.

Now that you can nest method definitions, perhaps it makes sense to
revisit this: if method definitions are now more like normal statements,
it would seem to make sense to have them return something. In an ideal
world it would return an UnboundMethod object, but a Symbol would be
cheaper in terms of runtime overhead.

But I very much like the idea of doing this: there are many times I’d
like to decorate a method definition, for example:

memoize def meth(a, b) ...

end


soap(Integer, String, def meth(aNumber, aString)
end)

etc…

So, any chance you might reconsider??? :slight_smile:

Cheers

Dave

···

In message “Re: public/protected/private syntax” > on 03/05/20, “Robert Klemme” bob.news@gmx.net writes:

Hi,

···

In message “Re: public/protected/private syntax” on 03/05/20, Dave Thomas dave@pragprog.com writes:

So, any chance you might reconsider??? :slight_smile:

Once I decide, it would cost me a few minutes. But we have to discuss
and get consensus about what “def” returns (is Symbol OK for you guys?).

In addition, we also have to re-visit return values from “class” and
“module” statements.

						matz.

Yukihiro Matsumoto wrote:

Hi,

So, any chance you might reconsider??? :slight_smile:

Once I decide, it would cost me a few minutes. But we have to discuss
and get consensus about what “def” returns (is Symbol OK for you guys?).

What would be the impact of having it return an unbound method? Is there
any way to optimize this so that it only generates the return object if
it knows that it is being used?

In addition, we also have to re-visit return values from “class” and
“module” statements.

Don’t they already return the value of the last statement executed? This
seems to be a Good Thing, as it is flexible. This way of doing things
obviously doesn’t apply to ‘def’, which doesn’t execute its body at
‘compile’ time.

Cheers

Dave

···

In message “Re: public/protected/private syntax” > on 03/05/20, Dave Thomas dave@pragprog.com writes: