Last active
May 17, 2016 10:23
-
-
Save yitsushi/e5a287fd32c12a1ac7dcfb2542c3ab19 to your computer and use it in GitHub Desktop.
Ruby public, protected, private calls
This file contains hidden or 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 A | |
public | |
def pub_test | |
puts "A::pub_test" | |
print " ->" | |
prot_test | |
print " ->" | |
priv_test | |
end | |
def child_calls | |
puts " === Calls from A on B" | |
b = B.new | |
b.pub_test | |
b.prot_test rescue puts "b.prot failed" | |
b.priv_test rescue puts "b.priv failed" | |
end | |
protected | |
def prot_test | |
puts "A::prot_test" | |
end | |
private | |
def priv_test | |
puts "A::priv_test" | |
end | |
end | |
class B < A | |
public | |
def pub_test | |
puts "B::pub_test" | |
print " ->" | |
super | |
print " +>" | |
prot_test | |
print " +>" | |
priv_test | |
end | |
end | |
class C < A | |
public | |
def priv_test | |
puts " == super in priv" | |
super | |
print " ->" | |
prot_test | |
end | |
end | |
b = B.new | |
b.pub_test | |
b.prot_test rescue puts "b.prot failed" | |
b.priv_test rescue puts "b.priv failed" | |
a = A.new | |
a.child_calls | |
c = C.new | |
c.priv_test | |
### Output | |
# B::pub_test | |
# ->A::pub_test | |
# ->A::prot_test | |
# ->A::priv_test | |
# +>A::prot_test | |
# +>A::priv_test | |
# b.prot failed | |
# b.priv failed | |
# === Calls from A on B | |
# B::pub_test | |
# ->A::pub_test | |
# ->A::prot_test | |
# ->A::priv_test | |
# +>A::prot_test | |
# +>A::priv_test | |
# A::prot_test | |
# b.priv failed | |
# == super in priv | |
# A::priv_test | |
# ->A::prot_test | |
### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment