Skip to content

Instantly share code, notes, and snippets.

@djberg96
Created April 7, 2011 22:09
Show Gist options
  • Save djberg96/908869 to your computer and use it in GitHub Desktop.
Save djberg96/908869 to your computer and use it in GitHub Desktop.
You want final? I got yer final right here, pal!
# 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
@erikh
Copy link

erikh commented Apr 7, 2011

golf clap

@djberg96
Copy link
Author

djberg96 commented Apr 8, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment