Skip to content

Instantly share code, notes, and snippets.

@garybernhardt
Created August 24, 2011 05:21
Show Gist options
  • Save garybernhardt/1167357 to your computer and use it in GitHub Desktop.
Save garybernhardt/1167357 to your computer and use it in GitHub Desktop.
CantTouchThis = Class.new do
def to_s
'<CantTouchThis>'
end
end.new
class Stripper
def self.strip(klass)
klass.class_eval do
Stripper::constants_to_remove.each do |c|
self.const_set c.to_sym, CantTouchThis
end
def self.import *names
names.each do |name|
constant = Stripper::const_get(name)
self.const_set(name, constant)
end
end
end
end
def self.constants_to_remove
exceptions = %w{Object Class Module}
Object.constants - exceptions
end
end
class Joe
Stripper::strip(self)
import :File
def self.get_io
IO
end
def self.get_file
File
end
end
# These print the constants; they still exist
puts IO.inspect
puts File.inspect
# This prints <CantTouchThis>; the constant is gone inside Joe
puts Joe.get_io.inspect
# This prints File; Joe imports it, so it can see it
puts Joe.get_file.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment