Created
September 13, 2011 04:27
-
-
Save TwP/1213116 to your computer and use it in GitHub Desktop.
monkey-proof your ruby code
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
# This is a very simple-minded and inelegant way to prevent others from | |
# modifying (monkey patching) your code. A global method "monkey_proof" is | |
# provided that can be used to prevent monkey patching on most any Object or | |
# Class. | |
# | |
moddule MonkeyProof | |
VERSION = '1.0.0' | |
def self.included( other ) | |
return if other.frozen? | |
if Class === other | |
other.instance_eval <<-CODE | |
def new( *args, &block ) | |
i = super | |
class << i; self.freeze; end | |
return i | |
end | |
CODE | |
eval <<-CODE | |
class #{other.name} | |
alias :_dup :dup | |
private :_dup | |
def dup | |
i = :_dup | |
class << i; self.freeze; end | |
i | |
end | |
end | |
CODE | |
end | |
other.freeze | |
end | |
def self.extended( other ) | |
if Module === other | |
included other | |
else | |
class << other; self.freeze; end | |
end | |
other | |
end | |
end # module MonkeyProof | |
module Kernel | |
def monkey_proof( obj ) | |
MonkeyProof.extended obj | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One step closer to Planet of The Apes.