Feature #6539

public and private for core methods

Added by Marc-Andre Lafortune over 3 years ago. Updated almost 3 years ago.

[ruby-core:45395]
Status:Closed
Priority:Normal
Assignee:Nobuyoshi Nakada

Description

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

For example:

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

Another example:

module M
def foo
42
end
end

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

Reverse example:

{}.method_missing :foo # => private method `method_missing' called
{}.respond_to_missing? :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
#initialize_clone
#initialize_dup
#respond_to_missing?
Rational & Complex
#marshal_dump
#marshal_load
Time
#dump
.
load

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

Should be made public:

Object
#remove_instance_variable
Module
#attr
#attr_reader
#attr_writer
#attr_accessor
#remove_const
#include
#remove_method
#undef_method
#alias_method
#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 almost 3 years ago

vm_method.c: make initialize methods private

  • id.c (Init_id), template/id.h.tmpl: add initialize_{copy,clone,dup} and respond_to_missing?.
  • vm_method.c (rb_method_entry_make): make above methods private. [Feature #6539]

Revision 38113
Added by Nobuyoshi Nakada almost 3 years ago

vm_method.c: make initialize methods private

  • id.c (Init_id), template/id.h.tmpl: add initialize_{copy,clone,dup} and respond_to_missing?.
  • vm_method.c (rb_method_entry_make): make above methods private. [Feature #6539]

Revision 38114
Added by Nobuyoshi Nakada almost 3 years ago

object.c: make remove_instance_variable public

  • object.c (Init_Object): make remove_instance_variable public. [Feature #6539]

Revision 38114
Added by Nobuyoshi Nakada almost 3 years ago

object.c: make remove_instance_variable public

  • object.c (Init_Object): make remove_instance_variable public. [Feature #6539]

Revision 38115
Added by Nobuyoshi Nakada almost 3 years ago

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

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

Revision 38115
Added by Nobuyoshi Nakada almost 3 years ago

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

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

Revision 38118
Added by Nobuyoshi Nakada almost 3 years ago

marshal.c: private methods

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

Revision 38118
Added by Nobuyoshi Nakada almost 3 years ago

marshal.c: private methods

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

Revision 38123
Added by Nobuyoshi Nakada almost 3 years ago

vm_method.c: private

  • vm_method.c (basic_obj_respond_to): call even if private. [Feature #6539]

Revision 38123
Added by Nobuyoshi Nakada almost 3 years ago

vm_method.c: private

  • vm_method.c (basic_obj_respond_to): call even if private. [Feature #6539]

Revision 38157
Added by Nobuyoshi Nakada over 2 years ago

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

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

Revision 38157
Added by Nobuyoshi Nakada over 2 years ago

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

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

History

#1 Updated by Marc-Andre Lafortune about 3 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 about 3 years ago

  • Status changed from Open to Assigned

#3 Updated by Yukihiro Matsumoto almost 3 years ago

  • Assignee changed from Yukihiro Matsumoto to Nobuyoshi Nakada

Concrete counter proposal:

Should be made private:
Object and descendants
#initialize_clone
#initialize_dup
#respond_to_missing?
Rational & Complex
#marshal_dump
#marshal_load
Time
#dump
.
load

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

Should be made public:

Object
#remove_instance_variable

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

Matz.

#4 Updated by Yusuke Endoh almost 3 years ago

  • Target version changed from 2.0.0 to next minor

#5 Updated by Nobuyoshi Nakada almost 3 years 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 (Init_id), template/id.h.tmpl: add initialize_{copy,clone,dup} and respond_to_missing?.
  • vm_method.c (rb_method_entry_make): make above methods private. [Feature #6539]

Also available in: Atom PDF