How to make private methods in Ruby
Ruby provides a mechanism for creating private methods. Methods which are not accessible outside of class. In order to define private method, you need to use “private” keyword in front of the method you are trying to make private.
class MyClass
private
def my_private_method
@private_variable
end
end
Private method are accessible by subclasses that makes private method implementation somewhat unique to Ruby when compared to other programming languages.
The “private” keyword is actually a method in Ruby and it belongs to Module class. Ruby has “protected” method as well which differ from “private”. The “protected” method is accessible form Class A to Class B unlike “private”
Note that all instance variables are private be default and accessible by subclass.