Skip to content

Instantly share code, notes, and snippets.

@TwP
Created September 13, 2011 04:27
Show Gist options
  • Select an option

  • Save TwP/1213116 to your computer and use it in GitHub Desktop.

Select an option

Save TwP/1213116 to your computer and use it in GitHub Desktop.
monkey-proof your ruby code
# 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
@mejibyte
Copy link
Copy Markdown

One step closer to Planet of The Apes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment