Skip to content

Instantly share code, notes, and snippets.

@TwP
Created March 19, 2009 20:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TwP/82025 to your computer and use it in GitHub Desktop.
Save TwP/82025 to your computer and use it in GitHub Desktop.
# 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