Last active
July 2, 2017 10:23
-
-
Save kinmor06/49fba41068f99358a61bcb3db3c8d404 to your computer and use it in GitHub Desktop.
Ruby class inheritance: prevent methods to be overriden
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
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