Last active
September 20, 2017 20:41
-
-
Save pboling/3e09bb9040e08d29718609ab467ef238 to your computer and use it in GitHub Desktop.
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 Foo | |
def calls_bar | |
bar + " from foo" | |
end | |
private | |
def bar | |
raise "define #{__method__} in subclasses" | |
end | |
end | |
class SubFooPrivateOverride < Foo | |
def initialize | |
puts "the method scope of bar is still private in this subclass: call without self #{bar}" | |
# The next call will fail | |
puts "the method scope of bar is still private in this subclass: call with self #{self.bar}" | |
rescue NoMethodError => e | |
puts "#{e.class}: #{e.message}" | |
end | |
private | |
def bar | |
"private subfoo bar" | |
end | |
end | |
class SubFooProtectedOverride < Foo | |
def initialize | |
puts "the method scope of bar is changed to protected in this subclass: call without self #{bar}" | |
puts "the method scope of bar is changed to protected in this subclass: call with self #{self.bar}" | |
rescue NoMethodError => e | |
puts "#{e.class}: #{e.message}" | |
end | |
protected | |
def bar | |
"protected subfoo bar" | |
end | |
end | |
puts SubFooPrivateOverride.new.calls_bar | |
puts SubFooProtectedOverride.new.calls_bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running this gives: