Skip to content

Instantly share code, notes, and snippets.

@adamkleingit
Last active December 18, 2015 15:59
Show Gist options
  • Save adamkleingit/5808269 to your computer and use it in GitHub Desktop.
Save adamkleingit/5808269 to your computer and use it in GitHub Desktop.
Define secret methods only in parent
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