Created
March 6, 2012 21:43
-
-
Save woods/1989123 to your computer and use it in GitHub Desktop.
Read existing Ruby source files into their own namespace after the fact
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
class A | |
@counter = 0 | |
def self.inc | |
@counter += 1 | |
end | |
end |
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
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 |
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
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