Created
January 4, 2010 16:00
-
-
Save fronx/268611 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
# Reincarnation for classes | |
class Class | |
def reincarnate | |
buried = Object.__send__(:remove_const, self.name) | |
Object.const_set(self.name, Class.new(buried)) | |
end | |
end | |
class Abc | |
def foo | |
"foo" | |
end | |
end | |
Abc.reincarnate | |
class Abc | |
def foo | |
puts super | |
end | |
end | |
Abc.new.foo | |
# Reincarnation for modules | |
class Module | |
def reincarnate | |
buried = Object.__send__(:remove_const, self.name) | |
Object.const_set(self.name, Module.new).__send__(:include, buried) | |
end | |
end | |
module Boo | |
def bar | |
"bar" | |
end | |
end | |
Boo.reincarnate | |
module Boo | |
def bar | |
puts super | |
end | |
end | |
class BooTest | |
include Boo | |
end | |
BooTest.new.bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment