-
-
Save tpope/35045 to your computer and use it in GitHub Desktop.
Now free of pesky instance_ methods!
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
module Kernel | |
def blank_context(*args, &block) | |
ivars = args.extract_options! | |
args.push(/^__/) | |
klass = Class.new do | |
instance_methods.each do |m| | |
undef_method(m) unless args.any? { |pattern| m =~ pattern } | |
end | |
end | |
klass.class_eval(&block) if block_given? | |
instance = klass.new | |
setter = Object.instance_method(:instance_variable_set).bind(instance) | |
ivars.each { |key, value| setter.call("@#{key}", value) } | |
instance | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment