Skip to content

Instantly share code, notes, and snippets.

@ryanking
Created October 29, 2010 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryanking/653962 to your computer and use it in GitHub Desktop.
Save ryanking/653962 to your computer and use it in GitHub Desktop.
module Stop
module CantTouchThis
def self.included(mod)
%w[instance_variable_get instance_variable_set].each do |m|
send(:protected, m)
end
eigenclass = class << mod; self; end
%w[const_set class_variable_get class_variable_set public_class_method attr attr_reader attr_writer].each do |m|
eigenclass.send(:protected, m)
end
mod.send(:define_method, :instance_eval) do
raise NoMethodError, "do do do, uh uh, duh duh. can't touch this"
end
%w[class_eval class_exec module_eval module_exec include extend alias_method unfreeze remove_const remove_method undef_method define_method].each do |m|
mod.class.send(:define_method, m) do
raise NoMethodError, "do do do, uh uh, duh duh. can't touch this"
end
end
mod.send(:protected, :send)
eigenclass.send(:protected, :send)
mod.freeze
end
end
end
def r
yield
puts "fail"
rescue => e
p e
end
module B
def self.included(_)
puts "fail"
end
end
class A
include Stop::CantTouchThis
r{class_eval '' }
r{class_exec {} }
r{module_eval '' }
r{include B }
r{extend B }
r{alias_method :class, :id }
r{define_method(:foo) {} }
r{ remove_const(:FOO) }
r{ remove_method(:id) }
end
r{ A.new.instance_eval '' }
r{ A.new.instance_variable_get(:@foo) }
r{ A.new.instance_variable_set(:@foo, :foo) }
r{ A.class_variable_get(:@foo) }
r{ A.new.class_variable_get(:@foo) }
r{ A.class_variable_set(:@foo, :bar) }
r{ A.new.class_variable_set(:@foo, :bar) }
r{ A.const_set(:B, :foo) }
r{ A.public_class_method(:send) }
r{ A.const_set(:FOO, :bar) }
r{ A.attr(:foo) }
r{ A.attr_reader(:foo) }
r{ A.attr_writer(:foo) }
r{ A.new.send(:id)}
r{ A.send(:id)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment