Created
January 24, 2011 00:38
-
-
Save jeffkreeftmeijer/792624 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# A solution to https://gist.github.com/792624/0df60239d007782c366445f6d86700382386716b | |
class Object | |
def clone_constants_from(victim) | |
victim.constants.each do |constant| | |
send(:remove_const, constant) | |
const_set(constant, victim.const_get(constant).clone) | |
victim.const_get(constant).clone_constants_from(const_get(constant)) | |
end | |
end | |
end | |
module Foo | |
class Bar | |
end | |
class Baz | |
class Bzz | |
end | |
end | |
end | |
Woo = Foo.clone | |
Woo.clone_constants_from(Foo) | |
puts Foo.object_id == Woo.object_id | |
puts Foo::Bar.object_id == Woo::Bar.object_id | |
puts Foo::Baz.object_id == Woo::Baz.object_id | |
puts Foo::Baz::Bzz.object_id == Woo::Baz::Bzz.object_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment