Created
April 7, 2011 22:09
-
-
Save djberg96/908869 to your computer and use it in GitHub Desktop.
You want final? I got yer final right here, pal!
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
# Make your classes final. Why? Because we can. | |
module Final | |
class Error < RuntimeError; end | |
def self.included(mod) | |
# Prevent subclassing, except implicity subclassing from Object. | |
def mod.inherited(sub) | |
raise Error, "cannot subclass #{self}" unless self == Object | |
end | |
# Freeze all instance methods and class methods. | |
def mod.method_added(sym) | |
if self.instance_methods.include?(sym) || self.instance_methods.include?(sym.to_s) | |
raise Error, "instance method '#{sym}' already defined" | |
end | |
if self.methods.include?(sym) || self.methods.include?(sym.to_s) | |
raise Error, "singleton method '#{sym}' already defined" | |
end | |
end | |
end | |
end |
Author
djberg96
commented
Apr 8, 2011
via email
Thanks, I try. :)
…On 4/7/11 5:17 PM, erikh wrote:
_golf clap_
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment