Last active
August 29, 2015 14:08
-
-
Save Fire-Dragon-DoL/541cae38404bab5b9527 to your computer and use it in GitHub Desktop.
Concerning Ruby `protected` access modified
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
#!/usr/bin/env ruby | |
class A | |
def foo(other) | |
puts "self: #{ bar }" | |
puts "other: #{ other.bar }" | |
end | |
protected | |
def bar | |
"a" | |
end | |
end | |
class B < A | |
protected | |
def bar | |
"b" | |
end | |
end | |
class C < A | |
def bar | |
"c" | |
end | |
end | |
a = A.new | |
b = B.new | |
c = C.new | |
a.foo c | |
b.foo a | |
b.foo c | |
c.foo a | |
a.foo b # => raises 7:in `foo': protected method `bar' called for #<B:0x00000002486730> (NoMethodError) | |
c.foo b # => raises 7:in `foo': protected method `bar' called for #<B:0x00000001ffa770> (NoMethodError) |
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
#!/usr/bin/env ruby | |
class A | |
def foo(other) | |
puts "self: #{ call_bar }" | |
puts "other: #{ other.call_bar }" | |
end | |
protected | |
def call_bar | |
bar | |
end | |
def bar | |
"a" | |
end | |
end | |
class B < A | |
protected | |
def bar | |
"b" | |
end | |
end | |
class C < A | |
def bar | |
"c" | |
end | |
end | |
a = A.new | |
b = B.new | |
c = C.new | |
a.foo c | |
b.foo a | |
b.foo c | |
c.foo a | |
a.foo b | |
c.foo b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment