Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Fire-Dragon-DoL/541cae38404bab5b9527 to your computer and use it in GitHub Desktop.
Save Fire-Dragon-DoL/541cae38404bab5b9527 to your computer and use it in GitHub Desktop.
Concerning Ruby `protected` access modified
#!/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)
#!/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