Load file into dynamic module

I’m trying to load ruby files into dynamically created modules. The code
is:

test1.rb:

class Test < A::Test
def run
puts “I’m test1.rb class!”
end
end
Test.new

test2.rb:

class Test < A::Test
def run
puts “I’m test2.rb class!”
end
end
Test.new

test.rb:

module A

$objs = Array.new

class Test
def initialize
$objs.push(self)
end
end

end

Module.new do |mod|
require_relative “test1.rb”
end

Module.new do |mod|
require_relative “test2.rb”
end

for o in $objs
o.run
end

Unfortunately it displays:

I’m test2.rb class!
I’m test2.rb class!

while I would expect it to display:

I’m test1.rb class!
I’m test2.rb class!

What is the reason? It works fine if module is defined statically at the
top of the included file. Why does ruby override my run method for the
first class? The class name is the same but classes are
defined in different modules so there should be no clash for run method.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs