Skip to content

Instantly share code, notes, and snippets.

@kinmor06
Last active July 2, 2017 10:23
Show Gist options
  • Save kinmor06/49fba41068f99358a61bcb3db3c8d404 to your computer and use it in GitHub Desktop.
Save kinmor06/49fba41068f99358a61bcb3db3c8d404 to your computer and use it in GitHub Desktop.
Ruby class inheritance: prevent methods to be overriden
class Product
def self.inherited(sub)
sub.class_eval do
def self.method_added(name)
if [:method1, :method2].include?(name)
remove_method name
raise Exception, "Can't override #{name} method"
end
end
end
end
end
class CreditCard < Product
def method1
end
end
# will return `Exception: Can't override method1 method`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment