Created
March 19, 2009 20:10
-
-
Save TwP/82025 to your computer and use it in GitHub Desktop.
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
# require 'monkey-proof' | |
# | |
# class Foo | |
# def foo() puts 'foo'; end | |
# end | |
# monkey_proof Foo | |
# | |
# class Foo | |
# include Enumerable | |
# end | |
# => TypeError: can't modify frozen object | |
# | |
# foo = Foo.new | |
# foo.extend Enumerable | |
# => TypeError: can't modify frozen object | |
# | |
module 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 | |
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