How to dynamically load class using namespaces/subdirectory in Ruby/Rails? How to dynamically load class using namespaces/subdirectory in Ruby/Rails? ruby ruby

How to dynamically load class using namespaces/subdirectory in Ruby/Rails?


try using constantize instead:

module Wtf  class Damm  endend#=> nil'Wtf::Damm'.constantize#=> Wtf::DammObject.const_get 'Wtf::Damm'#=> Wtf::Damm


Object does not know a constant named Worker::Deployer1, which is why Object.const_get 'Worker::Deployer1' doesn't work. Object only knows a constant Worker. What does work is Worker.const_get 'Deployer1'.

Vlad Khomisch's answer works, because if you look at the implementation of constantize, this is exactly what it does: it splits the string on '::' and recursively const_get's.