Visibility

    Methods can be marked as private or protected.

    A private method can only be invoked without a receiver, that is, without something before the dot. The only exception is self as a receiver:

    1. class Employee < Person
    2. def say_bye
    3. say "bye" # OK
    4. end
    5. end

    Private types can only be referenced inside the namespace where they are defined, and never be fully qualified.

    1. class Foo
    2. private class Bar
    3. end
    4. Bar # OK
    5. Foo::Bar # Error
    6. end
    7. Foo::Bar # Error

    private can be used with class, module, lib, enum, alias and constants:

    1. instances of the same type as the current type
    2. instances in the same namespace (class, struct, module, etc.) as the current type
    1. # Example of 1
    2. class Person
    3. protected def say(message)
    4. puts message
    5. end
    6. def say_hello
    7. say "hello" # OK, implicit self is a Person
    8. self.say "hello" # OK, self is a Person
    9. other = Person.new "Other"
    10. other.say "hello" # OK, other is a Person
    11. end
    12. def make_a_person_talk
    13. person = Person.new
    14. person.say "hello" # Error: person is a Person but current type is an Animal
    15. end
    16. end
    17. one_more = Person.new "One more"
    18. one_more.say "hello" # Error: one_more is a Person but current type is the Program
    19. # Example of 2
    20. module Namespace
    21. class Foo
    22. protected def foo
    23. puts "Hello"
    24. end
    25. end
    26. class Bar
    27. def bar
    28. # Works, because Foo and Bar are under Namespace
    29. Foo.new.foo
    30. end
    31. end
    32. end
    33. Namespace::Bar.new.bar

    A protected method can only be invoked from the scope of its class or its descendants. That includes the class scope and bodies of class methods and instance methods of the same type the protected method is defined on, as well as all types including or inherinting that type and all types in that namespace.

    1. class Parent
    2. protected def self.protected_method
    3. end
    4. Parent.protected_method # OK
    5. def instance_method
    6. Parent.protected_method # OK
    7. Parent.protected_method # OK
    8. end
    9. end
    10. class Child < Parent
    11. Parent.protected_method # OK
    12. def instance_method
    13. Parent.protected_method # OK
    14. end
    15. def self.class_method
    16. Parent.protected_method # OK
    17. end
    18. end
    19. class Parent::Sub
    20. Parent.protected_method # OK
    21. def instance_method
    22. Parent.protected_method # OK
    23. end
    24. def self.class_method
    25. Parent.protected_method # OK
    26. end
    27. end

    A private top-level method is only visible in the current file.

    1. require "./one"
    2. greet # undefined local variable or method 'greet'

    A private top-level type is only visible in the current file.

    1. private class Greeter
    2. def self.greet
    3. "Hello"
    4. end
    5. end
    6. Greeter.greet # => "Hello"