Project

General

Profile

Actions

Feature #9992

open

Access Modifiers (Internal Interfaces)

Added by dsferreira (Daniel Ferreira) almost 10 years ago. Updated over 6 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:63391]

Description

Hi,

I would like to discuss with you the concept of Internal Interfaces.

Currently ruby offers three access modifiers choices to set methods visibility:

  • public
  • protected
  • private

Public methods define what we may call the Public Interface.
Private methods are private to the class and its subclasses.
Protected methods are public for the subclasses.

I would like to work with a new access modifier that would allow the creation of Internal methods.

Internal methods would be object methods that could only be called within the namespace.

Ex:

module Foo; end

class Foo::Bar

  def baz 
     puts baz
  end

  internal :baz

end

class Foo::Qux

  def baz
     ::Foo::Bar.new.baz
  end

  public :baz

end

What about this use case:

module Foo; end

##
# Template class
#
# ==== Example
#         
#          Foo::SubClass.run(:arg1)
#          #=> ...

class Foo::Base

  def initialize(arg)
    @arg = arg
  end
  
  def self.run(arg)
    self.new(arg).perform
  end

  public_class_method :run

  ##
  # Override abstract method

  def perform
    raise NotImplementedError
  end

  internal :perform

end

##
# Test subclass.
# 
# ==== Example
#
#          Foo::Bar.run("Hello!")
#          # => My argument is Hello!

class Foo::Bar < Foo::Base

  def perform
    puts "My argument is: " + @arg
  end

  internal :perform

end

Is this something that we can think about in a future implementation of ruby?
An extra feature that would not break backward compatibility.

Cheers,
Daniel

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0