Public, Private, and Protected Methods
Ruby controls method visibility using public
, private
, and protected
keywords.
- public: Accessible from anywhere
- private: Only accessible within the defining class
- protected: Accessible within class and subclasses
Example:
class Secret
def public_method
puts "Visible"
end
private
def private_method
puts "Hidden"
end
end