Skip to content

Instantly share code, notes, and snippets.

@tpope
Forked from nakajima/blank_context.rb
Created December 12, 2008 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpope/35045 to your computer and use it in GitHub Desktop.
Save tpope/35045 to your computer and use it in GitHub Desktop.
Now free of pesky instance_ methods!
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