About class methods visibility (public/private)

Hi all,

Here are some questions regarding the visibility of class methods :

1/ why public, protected, private methods cannot be used for defining
the visibility of class methods in addition to defining the visibility
of instance methods ? Is there a technical reason or a conceptual reason
for this ?

2/ why the protected_class_method is missing ?
The only methods are private_class_method and public_class_method.

3/ The naming of these methods are quite confusing. Each time I read
them I understand “give me the private/public class method …”. The
naming is too/very close to private_methods, public_methods, …
I would prefer something like class_private, class_protected,
class_public.

Thanks for any clarification.

Is there anybody for answering my questions ?

Thanks in advance.

Ruby A. [email protected] wrote:
Hi all,

Here are some questions regarding the visibility of class methods :

1/ why public, protected, private methods cannot be used for defining the visibility of class methods in addition to defining the visibility of instance methods ? Is there a technical reason or a conceptual reason for this ?

2/ why the protected_class_method is missing ?
The only methods are private_class_method and public_class_method.

3/ The naming of these methods are quite confusing. Each time I read them I understand “give me the private/public class method …”. The naming is too/very close to private_methods, public_methods, …
I would prefer something like class_private, class_protected, class_public.

Hi,

In message “Re: About class methods visibility (public/private)”
on Wed, 21 Mar 2007 22:46:07 +0900, Ruby A.
[email protected] writes:

| 1/ why public, protected, private methods cannot be used for defining the visibility of class methods in addition to defining the visibility of instance methods ? Is there a technical reason or a conceptual reason for this ?

I am not sure what you mean here by “defining the visibility of class
methods in addition to defining the visibility of instance methods”.
Can you elaborate?

If you want to change class method visibility, use singleton class
notation, “class <<SomeClass”.

| 2/ why the protected_class_method is missing ?
| The only methods are private_class_method and public_class_method.

We don’t have private_class_methods nor public_class_methods in Ruby.
Are confusing with something else?

| 3/ The naming of these methods are quite confusing. Each time I read them I understand “give me the private/public class method …”. The naming is too/very close to private_methods, public_methods, …
| I would prefer something like class_private, class_protected, class_public.

What do you confuse with what? For me, class_private etc. mean nothing.

Examining your questions before asking something is a tip to get
useful answers.

          matz.

Matz,

First, I’m very glad that you answered my mail.

Yukihiro M. [email protected] wrote:

1/ why public, protected, private methods cannot be used for
defining the visibility of class methods in addition to defining the
visibility of instance methods ? Is there a technical reason or a
conceptual reason for this ?

I am not sure what you mean here by “defining the visibility of class
methods in addition to defining the visibility of instance methods”.
Can you elaborate?

Sure. I wrote the following code a few days ago :

class C
def one
puts “One”
end

def self.other_one
puts “Other one”
end

private

def two
puts “Two”
end

def self.other_two
puts “Other two”
end

private_class_method :other_two # added later

end

What I basically expected is that the private method will make the
class method “other_two” private as it makes private the instance method
“two”.
I realized later the usefulness of the private_class_method so I added
the commented line afterwards.
What I’d like if possible is that the private method (and its friend
protected and public) works also for class methods.

If you want to change class method visibility, use singleton class
notation, "class <<

I agree. But the public_class_method and private_class_method does the
job without having to go into the singleton class.

| 2/ why the protected_class_method is missing ?
| The only methods are private_class_method and public_class_method.

We don’t have private_class_methods nor public_class_methods in Ruby.
Are confusing with something else?

Yes. I just mean that there is no method for making a class method
protected like there are ones for making a class method private or
public.
What is missing is a protected_class_method similar as
private_class_method and public_class_method.

| 3/ The naming of these methods are quite confusing. Each time I
read them I understand “give me the private/public class method …”.
The naming is too/very close to private_methods, public_methods, …
| I would prefer something like class_private, class_protected, class_public.

What do you confuse with what? For me, class_private etc. mean nothing.

You have methods like private_methods / private_instance_methods, …
returning the private methods / private instance methods of an
object/class.
private_class_method sounds like return me the private class method of

This is a bit confusing.

For my suggestion :
like you have private, protected, public for defining the visibility
of instance methods, you will have class_private, class_protected,
class_public for the visibility of class methods.
The prefix class_ is a hint for indicating that these methods work for
class methods.

Examining your questions before asking something is a tip to get
useful answers.
matz.

It seems that my questions were not clear initially.
I hope that it is now more clear.

You mean you want Java or C++ or something else.
It’s Ruby.
All methods are either public, protected or private.
It’s a very simple system. It doesn’t need further complication.

John J. [email protected] wrote:

You mean you want Java or C++ or something else.
It’s Ruby.

I don’t really understand what you mean.
Please, read again my post.
I love Ruby (my pseudo is Ruby A.) and public_class_method and
private_class_method are already parts of Ruby !

All methods are either public, protected or private.
It’s a very simple system. It doesn’t need further complication.

I completely agree. My first question was indeed why public,
protected, private “statement” (they are in fact methods) does not work
for class methods ?

That’s all.

From: Ruby A. [mailto:[email protected]]

Sent: Thursday, March 22, 2007 9:29 AM

John J. [email protected] wrote:

You mean you want Java or C++ or something else.
It’s Ruby.

I don’t really understand what you mean.
Please, read again my post.
I love Ruby (my pseudo is Ruby A.) and public_class_method and
private_class_method are already parts of Ruby !

No, they’re not:
C:>irb
irb(main):001:0> Object.public_class_methods
NoMethodError: undefined method public_class_methods' for Object:Class from (irb):1 irb(main):002:0> Object.private_class_methods NoMethodError: undefined method private_class_methods’ for
Object:Class
from (irb):2

C:>irb -v
irb 0.9.5(05/04/13)

C:>ruby -v
ruby 1.8.5 (2006-08-25) [i386-mswin32]

Maybe you’re thinking of some module or library in Ruby on Rails? (I
don’t know if it includes those methods or not.)

I completely agree. My first question was indeed why public, protected,
private “statement” (they are in fact methods) does not work for class
methods ?

‘Class’ methods, like ‘class’ instance variables, are no different
from normal methods and instance variables. They are just defined in a
different scope.

Phrogz [email protected] wrote:

I don’t really understand what you mean.
Please, read again my post.
I love Ruby (my pseudo is Ruby A.) and public_class_method and
private_class_method are already parts of Ruby !

No, they’re not:
C:>irb
irb(main):001:0> Object.public_class_methods
NoMethodError: undefined method public_class_methods' for Object:Class from (irb):1 irb(main):002:0> Object.private_class_methods NoMethodError: undefined method private_class_methods’ for
Object:Class
from (irb):2

Of course, public_class_methods and private_class_methods do not exist
!
BUT public_class_method and private_class_method DO exist !! Do not
put an extra ‘s’ !

You can try within IRB :
Module.private_class_method
and it will work.
Likewise, you will have an explanation for the method with :
ri private_class_method
See also the Pickaxe book.

It seems that from the beginning, there is a lot of misunderstanding
on my questions !

On Mar 22, 9:51 am, “Phrogz” [email protected] wrote:

I love Ruby (my pseudo is Ruby A.) and public_class_method and
Object:Class
from (irb):2

My fault, I misread and thought I saw an extra ‘s’ on the end there.
Of course Module#public_class_method is included in the core of Ruby.

Apologies for the noise.

On Mar 22, 2:53 am, Yukihiro M. [email protected] wrote:

| 3/ The naming of these methods are quite confusing. Each time I read them I understand “give me the private/public class method …”. The naming is too/very close to private_methods, public_methods, …
| I would prefer something like class_private, class_protected, class_public.

What do you confuse with what? For me, class_private etc. mean nothing.

If I may attempt to be an interpreter, I think what the OP was saying
was if you look at this list…

Return a method by name

Module#method
Module#instance_method

Return array of method names

Module#methods
Module#singleton_methods
Module#instance_methods
Module#private_methods
Module#private_instance_methods
Module#protected_methods
Module#protected_instance_methods
Module#public_methods
Module#public_instance_methods

Change method ‘scope’

Module#private_class_method
Module#public_class_method

…you see that most methods with “method” in their name return
something about methods, but the last two cause a change to the method
‘scope’. I can see the confusion in particular between the first two
and the last two:

Klass.method( :foo ) #=> find a method
Klass.public_class_method( :foo ) #=> change scope
Klass.instance_method( :foo ) #=> find a method

The other question of the OP was “I know about three scopes in Ruby:
‘public’, ‘protected’, and ‘private’. However, I only see
#public_class_method and #private_class_method. For consistency,
shouldn’t there also be #protected_class_method.”

On Mar 22, 10:21 am, “Phrogz” [email protected] wrote:

On Mar 22, 2:53 am, Yukihiro M. [email protected] wrote:

| I would prefer something like class_private, class_protected, class_public.

What do you confuse with what? For me, class_private etc. mean nothing.

I used to say to budding programmers “properties should be named as
nouns, methods should be named as verbs”. With Ruby craftily making
all public access go through methods, I’d have to revise that to
something like:

“Methods with no side-effects whose purpose is to return a value
should be named as nouns. Methods whose purpose is to cause a side
effect should be named as verbs or imperatives.”

So my suggestion would be to rename the two methods in question as
something like:

Module#public_class_method => Module#make_methods_public
=> Module#publicize_methods

Module#private_class_method => Module#make_methods_private
=> Module#privatize_methods

(I’m not sure if I like the method name to use “methods” or “method”,
given that it can take more than one argument.)

wow. Good interpretation!
Explicitly typing names of things helps too.

Phrogz [email protected] wrote:

…you see that most methods with “method” in their name return
something about methods, but the last two cause a change to the method
‘scope’. I can see the confusion in particular between the first two
and the last two:

Klass.method( :foo ) #=> find a method
Klass.public_class_method( :foo ) #=> change scope
Klass.instance_method( :foo ) #=> find a method

Exactly !

The other question of the OP was “I know about three scopes in Ruby:
‘public’, ‘protected’, and ‘private’. However, I only see
#public_class_method and #private_class_method. For consistency,
shouldn’t there also be #protected_class_method.”
This was my second question.

So my suggestion would be to rename the two methods in question as
something like:

Module#public_class_method => Module#make_methods_public
=> Module#publicize_methods

Module#private_class_method => Module#make_methods_private
=> Module#privatize_methods

My opinion is that the change of scope should be kept/indicated.
Your proposals should include ‘class’ somewhere.

(I’m not sure if I like the method name to use “methods” or “method”,
given that it can take more than one argument.)

In my opinion, class_private, class_public, and class_protected names
have the following benefits :

  • they are very close to the private, public and protected methods.
  • they cover one or multiple arguments

But similar names will also be great !

Yukihiro M. [email protected] wrote:

There’s no need for new methods. private_class_method and others are
utility method for people who really love the term ‘class methods’.

I agree.
But as I indicated with a previous post, there is one such utility
method that is currently missing. There is no protected_class_method for
making a class method protected.

In addition, the current naming of the 2 existing utility methods
(private_class_method and public_class_method) is very confusing.
Please take a look at the post on this thread from Gavin who
“interprets” my request and states more clearly the naming issue.

So I do not request a lot of fresh new methods.
I just request one new method and a renaming of 2 existing methods.

On Mar 23, 4:39 am, Yukihiro M. [email protected] wrote:

There’s no need for new methods. private_class_method and others are
utility method for people who really love the term ‘class methods’.

Ick! When did those get in there? I’m almost glad I never noticed
before. Why bother? Just give us a method for singleton/eigenclass/
metaclass whatever you want to call the “context” and allow:

context.public :foo

But if they’ve got to stay, please, at least consider renaming them
something like class_public and class_private.

t.

matz, your example was so elegantly simple that I want to say you have
my vote to just drop the methods in question–at least from core.
There is plenty of Java to go around already.

Hi,

In message “Re: About class methods visibility (public/private)”
on Fri, 23 Mar 2007 02:34:51 +0900, Ruby A.
[email protected] writes:

|In my opinion, class_private, class_public, and class_protected names have the following benefits :
|- they are very close to the private, public and protected methods.
|- they cover one or multiple arguments
|
|But similar names will also be great !

In my opinion, if you want to change the visibility, you’d better to
use singleton class notation, for class methods are singleton methods
for class objects after all.

class Foo
class <<Foo
# class method foo
def foo
end
# visibility private
private :foo
end
# instance method foo
def foo
end
# visibility private
private :foo
end

There’s no need for new methods. private_class_method and others are
utility method for people who really love the term ‘class methods’.

          matz.

Hi,

In message “Re: About class methods visibility (public/private)”
on Fri, 23 Mar 2007 22:45:14 +0900, Ruby A.
[email protected] writes:

| I totally agree. If the current two utility methods are to stay in the Ruby core, please :
| - add the missing one for protected visibility

If you understand the current behavior of protected visibility, you
will understand why protected_class_method is not useful at all.
That’s why we don’t have it.

| - change the naming of these methods for removing the confusion
| for the overall consistency.

If we can drop them, why bother to rename? Consistency does not come
first in the Ruby’s design.

          matz.

Trans [email protected] wrote:
But if they’ve got to stay, please, at least consider renaming them
something like class_public and class_private.

Paul S. [email protected] wrote:
matz, your example was so elegantly simple that I want to say you
have
my vote to just drop the methods in question–at least from core.
There is plenty of Java to go around already.

I totally agree. If the current two utility methods are to stay in the
Ruby core, please :

  • add the missing one for protected visibility
  • change the naming of these methods for removing the confusion
    for the overall consistency.

Otherwise, just remove the 2 existing methods.

Hi,

In message “Re: About class methods visibility (public/private)”
on Fri, 23 Mar 2007 18:34:22 +0900, “Trans” [email protected]
writes:

|Ick! When did those get in there? I’m almost glad I never noticed
|before. Why bother? Just give us a method for singleton/eigenclass/
|metaclass whatever you want to call the “context” and allow:
|
| context.public :foo

(Unlike Java and such) visibility in Ruby are very trivial, so I don’t
want to make things more complex for the sake of visibility. I’d
rather remove them altogether unless compatibility problem arise.

|But if they’ve got to stay, please, at least consider renaming them
|something like class_public and class_private.

if public/private/protected are called without method names, it
switches the default visibility. public_class_method and such does
not this trick. Renaming public_class_method to class_public does not
sound right for me, since it strongly suggest the changing the
default.

          matz.