title: How to dynamically load class using namespaces/subdirectory in Ruby/Rails?
body: <p>In my Rails 3.1 app (with Ruby 1.9), I have a <strong>Deployer1</strong> class that is in a <strong>worker</strong> subdirectory below the <strong>model</strong> directory</p>
  
  <p>I am trying to load/instantiate this class dynamically with this code:</p>
  
  <pre><code>clazz = item.deployer_class  # deployer_class is the class name in a string
  deployer_class = Object.const_get clazz
  deployer = deployer_class.new
  </code></pre>
  
  <p>If I dont use namespaces, eg something global like this:</p>
  
  <pre><code>class Deployer1
  end
  </code></pre>
  
  <p>Then it works fine (deployer_class="Deployer1") - it can load the class and create the object.</p>
  
  <p>If I try and put it into a module to namespace it a bit, like this:</p>
  
  <pre><code>module Worker
      class Deployer1
      end
  end
  </code></pre>
  
  <p>It doesnt work (deployer_class="Worker::Deployer1") - gives an error about missing constant, which I believe means it cannot find the class.</p>
  
  <p>I can access the class generally in my Rails code in a static way (Worker::Deployer1.new) - so Rails is configured correctly to load this, perhaps I am loading it the wrong way...</p>
  
  <p>EDIT:
  So, as per Vlad's answer, the solution I went for is:</p>
  
  <pre><code>deployer_class.constantize.new
  </code></pre>
  
  <p>Thanks
  Chris</p>