Forum: Ruby-core [Open] Method that is visible only within a certain module/class

43421679c356f438c69bf6947e62c4ec?d=identicon&s=25 Tsuyoshi S. (tsuyoshi_s)
on 2014-04-22 11:57
(Received via mailing list)
Issue #9768 has been reported by Tsuyoshi Sawada.

----------------------------------------
Feature #9768: Method that is visible only within a certain module/class
https://bugs.ruby-lang.org/issues/9768

* Author: Tsuyoshi Sawada
* Status: Open
* Priority: Normal
* Assignee:
* Category:
* Target version:
----------------------------------------
Some frameworks/libraries monkeypatch their own methods on Ruby core
classes like `String`, `Hash`, `Array`, etc., and that is often causing
problems/concerns of name conflict.

Seeing that these custom methods are used only in the context of a
certain module/class, I request for a way to define a method (`foo`) on
a module/class (`A`) so that it will be visible only from within a
specified module/class (`B`) or its descendants. The following
illustrates this situation:

    A.new.foo # => undefined

    class B
      A.new.foo # => defined
      def bar
        A.new.foo # => defined
      end
      def self.baz
        A.new.foo # => defined
      end
    end

    class C < B
      A.new.foo # => defined
      def bar
        A.new.foo # => defined
      end
      def self.baz
        A.new.foo # => defined
      end
    end

I do not have a certain syntax for this in mind, but I think it can be
made much simpler, compared to the complicated syntax of refinement.

This is reminiscent of refinement, but they are pretty much different.

Refinement's purpose is to let certain methods be accessible from only a
certain file. A typical use case would be a library developer defining
their methods to be used from within the library and making such methods
inaccessible from the end user.

On the other hand, the idea I am proposing here is for making method
accessible from any file, but only within a certain module/class. A
typical use case would be defining a method to be used by an end user,
but only from within a context of certain module/class.
This topic is locked and can not be replied to.