Let's say I have this Ruby code in test.rb
module MyModule class TestClassA end class TestClassB def initialize a = Object.const_get('MyModule::TestClassA').new end end end
Here, some tests in the ruby ββshell started with irb -r test.rb:
ruby-1.8.7-p302 > MyModule => MyModule ruby-1.8.7-p302 > MyModule::TestClassA => MyModule::TestClassA ruby-1.8.7-p302 > MyModule::TestClassA.new => #<MyModule::TestClassA:0x10036bef0> ruby-1.8.7-p302 > MyModule::TestClassB => MyModule::TestClassB ruby-1.8.7-p302 > MyModule::TestClassB.new NameError: wrong constant name MyModule::TestClassA from ./test.rb:7:in `const_get' from ./test.rb:7:in `initialize' from (irb):1:in `new' from (irb):1
Why Object.const_get('MyModule::TestClassA').new
in the TestClassB
constructor not work, and MyModule::TestClassA.new
works in the console? I also tried Object.const_get('TestClassA').new
, but this does not work either.
StefanS
source share