Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Module#protected

() -> nil
(::Symbol method_name) -> ::Symbol
(::Symbol, ::Symbol, *::Symbol method_name) -> ::Array[::Symbol]
(::string method_name) -> ::string
(::interned, ::interned, *::interned method_name) -> ::Array[::interned]
(::Array[::interned]) -> ::Array[::interned]
protected → nil
protected(method_name) → method_name
protected(method_name, method_name, ...) → array
protected(array) → array

With no arguments, sets the default visibility for subsequently defined methods to protected. With arguments, sets the named methods to have protected visibility. String arguments are converted to symbols. An Array of Symbols and/or Strings is also accepted. If a single argument is passed, it is returned. If no argument is passed, nil is returned. If multiple arguments are passed, the arguments are returned as an array.

If a method has protected visibility, it is callable only where self of the context is the same as the method. (method definition or instance_eval). This behavior is different from Java’s protected method. Usually private should be used.

Note that a protected method is slow because it can’t use inline cache.

To show a private method on RDoc, use :doc: instead of this.

Ruby version changes

  1. 3.0