[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: About class methods visibility (public/private

Yukihiro Matsumoto

3/22/2007 8:54:00 AM

Hi,

In message "Re: About class methods visibility (public/private)"
on Wed, 21 Mar 2007 22:46:07 +0900, Ruby Admirer <ruby_admirer@yahoo.com> 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.

2 Answers

Gavin Kistner

3/22/2007 4:21:00 PM

0

On Mar 22, 2:53 am, Yukihiro Matsumoto <m...@ruby-lang.org> 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."

Gavin Kistner

3/22/2007 4:31:00 PM

0

On Mar 22, 10:21 am, "Phrogz" <g...@refinery.com> wrote:
> On Mar 22, 2:53 am, Yukihiro Matsumoto <m...@ruby-lang.org> 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.)