title: How to dynamically require other file and then create an object in Ruby?
body: <p>I have this source file structure:</p>
  
  <pre><code>-- root.rb
  |
  |- root -- |- a.rb
             |- b.rb
             |- c.rb
  </code></pre>
  
  <p>In <code>root.rb</code>, there is a class named <code>Root</code>, <code>a.rb</code> contains a class named <code>A</code>, and so on.</p>
  
  <p>I want the <code>Root</code> class to iterate the <code>root</code> fold, and dynamically create instance of class <code>A</code>, <code>B</code>, <code>C</code>.</p>
  
  <pre><code>class Root 
    def initialize
      objs = []
      dir = Dir.new('root')
      dir.each { |file|
          # Here file should be a.rb, b.rb, c.rb, etc.
          # I want to dynamically require "a.rb, and create and an object of A.
          # I know the following two lines must be wrong,
          # Could anybody give me the correct code?
          require file    
          objes &lt;&lt; ClassFromFilename(file).new 
      }
    end
  end
  </code></pre>
  
  <p>I'm looking for correct code to do "require" and creating object from file name. Please see code comment for details.</p>