Last active
February 28, 2016 21:20
-
-
Save KaoruDev/c7e18cb5dcb1a5c09db3 to your computer and use it in GitHub Desktop.
Ruby Protected vs Private
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Parent | |
def public_foo | |
"public foo" | |
end | |
protected | |
def pro_foo | |
"protected foo" | |
end | |
private | |
def priv_foo | |
"private foo" | |
end | |
end | |
class Child < Parent | |
def priv_foo | |
priv_foo | |
end | |
def pro_foo_of(a) | |
a.pro_foo | |
end | |
def priv_foo_of(a) | |
a.priv_foo | |
end | |
end | |
parent = Parent.new | |
parent.pro_foo # NoMethodError: protected method `pro_foo` | |
child = Child.new | |
child.priv_foo # => "private foo" | |
child.pro_foo_of(parent) #=> "protected foo" | |
child.priv_foo_of(parent) #=> # NoMethodError: protected method `pro_foo` | |
# Ref: http://stackoverflow.com/questions/9882754/what-are-the-differences-between-private-public-and-protected-methods |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment