Last active
December 18, 2015 15:59
-
-
Save adamkleingit/5808269 to your computer and use it in GitHub Desktop.
Define secret methods only in parent
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
module SecretMethods | |
def secret_def(name, &block) | |
klass = self | |
define_method name do | |
if self.class != klass | |
raise NoMethodError | |
else | |
yield | |
end | |
end | |
end | |
end | |
class Parent | |
extend SecretMethods | |
secret_def(:onlyhere) do | |
"bla bla" | |
end | |
end | |
class Son < Parent | |
secret_def(:onlythere) do | |
"blu blu" | |
end | |
end | |
class Grandson < Son | |
end | |
puts Parent.new.onlyhere | |
puts Son.new.onlythere | |
puts Son.new.onlyhere rescue nil | |
puts Grandson.new.onlyhere rescue nil | |
puts Grandson.new.onlythere rescue nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment