Forked from jeffkreeftmeijer/clone_module_incluing_classes.rb
Created
January 24, 2011 01:31
-
-
Save yoricksijsling/792665 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
# How does one deep clone a module including all its classes? | |
module Foo | |
class Bar | |
def self.baz | |
'foo' | |
end | |
end | |
end | |
puts Foo::Bar.baz # => 'foo' | |
# This solves the problem in this particular case. I'm not entirely sure if it will always work. | |
Woo = Module.new | |
Foo.constants.each { |c| Woo.const_set c, Foo.const_get(c).clone } | |
module Foo | |
class Bar | |
def self.baz | |
'bar' | |
end | |
end | |
end | |
puts Foo::Bar.baz # => 'bar' | |
puts Woo::Bar.baz # => 'foo' | |
puts Foo::Bar.object_id == Woo::Bar.object_id ? "Nope": "It works!" # => 'It works!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment