Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yoricksijsling/792665 to your computer and use it in GitHub Desktop.
Save yoricksijsling/792665 to your computer and use it in GitHub Desktop.
# 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