Last active
January 12, 2016 23:56
-
-
Save seanlinsley/d0809c93ce6e44ba6099 to your computer and use it in GitHub Desktop.
For slightly more future-proof monkey patches, raise an error if a method name starts being used upstream
This file contains hidden or 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 SomethingYouDontControl | |
| # def! :something_you_expect_not_to_exist do |arg| | |
| # arg + 1 | |
| # end | |
| # end | |
| # | |
| # I wanted to provide built-in memoization, but it's not currenlty possible | |
| # while preserving strict argument checking. https://bugs.ruby-lang.org/issues/9777 | |
| # | |
| module Kernel | |
| def def! name, &block | |
| raise "`#{name}` already exists at #{instance_method(name).source_location}" if method_defined? name | |
| define_method name, &block | |
| end | |
| def singleton_def! name, &block | |
| singleton_class.def! name, &block | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment