Feature #6539

public and private for core methods

Added by Marc-Andre Lafortune almost 2 years ago. Updated over 1 year ago.

[ruby-core:45395]
Status:Closed
Priority:Normal
Assignee:Nobuyoshi Nakada
Category:core
Target version:next minor

Description

I feel there are inconsistencies in which methods are public and which are private.

For example:

obj = []
prev = obj.instancevariableget(:@foo) # allowed
obj.instancevariableset(:@foo, nil) # allowed
# But these are almost equivalent to:
prev = obj.removeinstancevariable(:@foo)
# => NoMethodError: private method `removeinstancevariable' called for []:Array

Another example:

module M
def foo
42
end
end

M.modulefunction :foo # private method `modulefunction' called for M:Module
M.extend M # allowed
M.foo # => 42

Reverse example:

{}.methodmissing :foo # => private method `methodmissing' called
{}.respondtomissing? :foo, false # => allowed, why?

Which methods should be private is a different question for Ruby than for apps and libraries; the "real" private methods of Ruby are in C!
For Ruby, I feel that a method should be private if it is not meant to be called except by Ruby itself (callbacks, etc...), or if it's a "global" methods of Kernel that is meant to be called directly (i.e. puts instead of 42.puts)

Otherwise, it should be public. This includes methods like Module#include.

I don't know what the rationale was to make include and the like private.

I feel it is now quite common to use metaprogramming, e.g. to include modules from outside a class. It's part of a Class' API that it can be extended and modified, so these methods should be public.

Concrete proposal:

Should be made private:
Object and descendants
#initializeclone
#initialize
dup
#respondtomissing?
Rational & Complex
#marshaldump
#marshal
load
Time
#dump
.
load

Note that Delegate#initialize_{clone|dup} are already private

Should be made public:

Object
#removeinstancevariable
Module
#attr
#attrreader
#attr
writer
#attraccessor
#remove
const
#include
#removemethod
#undef
method
#aliasmethod
#public
#protected
#private
#module
function
#define_method


Related issues

Related to ruby-trunk - Feature #8846: Publicize Module#include Closed 08/31/2013

Associated revisions

Revision 38113
Added by Nobuyoshi Nakada over 1 year ago

vm_method.c: make initialize methods private

  • id.c (Initid), template/id.h.tmpl: add initialize{copy,clone,dup} and respondtomissing?.
  • vmmethod.c (rbmethodentrymake): make above methods private. [Feature #6539]

Revision 38114
Added by Nobuyoshi Nakada over 1 year ago

object.c: make removeinstancevariable public

  • object.c (InitObject): make removeinstance_variable public. [Feature #6539]

Revision 38115
Added by Nobuyoshi Nakada over 1 year ago

complex.c, time.c: make marshal methods private

  • complex.c (InitComplex), time.c (InitTime): make marshal methods private. [Feature #6539]

Revision 38118
Added by Nobuyoshi Nakada over 1 year ago

marshal.c: private methods

  • marshal.c (wobject, robject0): call private marshal methods. [Feature #6539]

Revision 38123
Added by Nobuyoshi Nakada over 1 year ago

vm_method.c: private

  • vmmethod.c (basicobjrespondto): call even if private. [Feature #6539]

Revision 38157
Added by Nobuyoshi Nakada over 1 year ago

random.c, rational.c: make marshal methods private

  • random.c (InitRandom), rational.c (InitRational): make marshal methods private. [Feature #6539]

History

#1 Updated by Marc-Andre Lafortune almost 2 years ago

  • Assignee set to Yukihiro Matsumoto

No comments, so I'm assigning to Matz.

There are more than 150 older issues assigned to Matz already.

#2 Updated by Yusuke Endoh almost 2 years ago

  • Status changed from Open to Assigned

#3 Updated by Yukihiro Matsumoto over 1 year ago

  • Assignee changed from Yukihiro Matsumoto to Nobuyoshi Nakada

Concrete counter proposal:

Should be made private:
Object and descendants
#initializeclone
#initialize
dup
#respondtomissing?
Rational & Complex
#marshaldump
#marshal
load
Time
#dump
.
load

Note that Delegate#initialize_{clone|dup} are already private

Should be made public:

Object
#removeinstancevariable

I think class/module operations should be done in the scope.

Matz.

#4 Updated by Yusuke Endoh over 1 year ago

  • Target version changed from 2.0.0 to next minor

#5 Updated by Nobuyoshi Nakada over 1 year ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r38113.
Marc-Andre, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


vm_method.c: make initialize methods private

  • id.c (Initid), template/id.h.tmpl: add initialize{copy,clone,dup} and respondtomissing?.
  • vmmethod.c (rbmethodentrymake): make above methods private. [Feature #6539]

Also available in: Atom PDF