Chapter 7 Access Control

Ruby allows you to modify an object’s state by calling one of its methods. Once you can control access to the methods, you can have controlled access to the object as well.

Ruby provides you with three levels of protection:

  • Public methods – This type of methods can be called by anyone. An example of this type is the class’ instance methods.
  • Protected methods – Only objects of the defining class and its subclasses can invoke this type of method.
  • Private methods – This type of method cannot be called by any other receiver. The receiver will always be self.

Example:

class AccessControl
    def AC1
    end
    protected 
        def AC2
        end
    private
        def AC3
        end
end

ACCL = AccessControl.new
ACCL.AC1
ACCL.AC2
ACCL.AC3

  • Currently 3.01/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
  Flag Inappropriate Content 0 comments