Created
November 20, 2008 05:27
-
-
Save nakajima/26948 to your computer and use it in GitHub Desktop.
on the fly blank slate classes
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