Skip to content

Instantly share code, notes, and snippets.

@woods
Created March 6, 2012 21:43
Show Gist options
  • Save woods/1989123 to your computer and use it in GitHub Desktop.
Save woods/1989123 to your computer and use it in GitHub Desktop.
Read existing Ruby source files into their own namespace after the fact
class A
@counter = 0
def self.inc
@counter += 1
end
end
m1 = Module.new
m1.module_eval(File.read('existing_ruby_class.rb'))
puts "m1:"
3.times do
puts m1::A.inc
end
m2 = Module.new
m2.module_eval(File.read('existing_ruby_class.rb'))
puts "m2:"
3.times do
puts m2::A.inc
end
puts "m1:"
3.times do
puts m1::A.inc
end
p A
m1:
1
2
3
m2:
1
2
3
m1:
4
5
6
asdf.rb:22:in `<main>': uninitialized constant A (NameError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment