Project

General

Profile

Feature #6539

public and private for core methods

Added by Marc-Andre Lafortune over 4 years ago. Updated 10 days ago.

Status:
Closed
Priority:
Normal
[ruby-core:45395]

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
Related to Ruby trunk - Feature #12697: Why shouldn't Module meta programming methods be public? Feedback

Associated revisions

Revision 38113
Added by Nobuyoshi Nakada about 4 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 about 4 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 about 4 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 about 4 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 about 4 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 about 4 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 about 4 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 about 4 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 about 4 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 about 4 years ago

marshal.c: private methods

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

Revision 38118
Added by Nobuyoshi Nakada about 4 years ago

marshal.c: private methods

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

Revision 38118
Added by Nobuyoshi Nakada about 4 years ago

marshal.c: private methods

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

Revision 38123
Added by Nobuyoshi Nakada about 4 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 about 4 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 about 4 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 about 4 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 about 4 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 about 4 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 [ruby-core:45437] Updated by Marc-Andre Lafortune over 4 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 [ruby-core:45593] Updated by Yusuke Endoh over 4 years ago

  • Status changed from Open to Assigned

#3 [ruby-core:48450] Updated by Yukihiro Matsumoto about 4 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 [ruby-core:48462] Updated by Yusuke Endoh about 4 years ago

  • Target version changed from 2.0.0 to next minor

#5 Updated by Nobuyoshi Nakada about 4 years ago

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

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]

#6 [ruby-core:78342] Updated by Koichi Sasada 10 days ago

  • Description updated (diff)

#7 Updated by Shyouhei Urabe 9 days ago

  • Related to Feature #12697: Why shouldn't Module meta programming methods be public? added

Also available in: Atom PDF