Skip to content

Instantly share code, notes, and snippets.

@seanlinsley
Last active January 12, 2016 23:56
Show Gist options
  • Select an option

  • Save seanlinsley/d0809c93ce6e44ba6099 to your computer and use it in GitHub Desktop.

Select an option

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
# 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