1

I'm new to writing big Ruby projects (only mods and application scripting till now). I've made a project with some files in the project root and others in subfolders, including a "Test" folder.

So far I've tried:

  • Adding all folders and subfolders to the load path in RubyMine.
  • Declaring all the files to be part of the same module.

I've thought of using require_relative for loading all files needed but it seems tiresome and very Java-esque... Is there a better way?

moshewe
  • 127
  • 1
  • 9

1 Answers1

2

Here is an example of a typical folder structure, its modules and how to include everything into the library. You would then only need to require 'lib' wherever you want to pull in the library for use.

# Root folder structure of project
lib.rb
./lib/a.rb
./lib/b.rb
./lib/b/c.rb

# lib.rb
require 'lib/a'
require 'lib/b'

module Lib
end

# ./lib/a.rb
module Lib
  module A
  end
end

# ./lib/b.rb
require 'lib/b/c'
module Lib    
  module B
  end
end

# ./lib/b/c.rb
module Lib
  module B
    module C
    end
  end
end
Dave N
  • 398
  • 1
  • 4
  • Also - all files (a.rb, b.rb, c.rb) in my case are in the same module, as each begins with the same module declaration. – moshewe Dec 06 '14 at 18:03
  • Oh, yes. Sorry I overlooked that. I will update to make it clearer that they all belong to the same module. – Dave N Dec 06 '14 at 19:07
  • Also, this is the way that libraries/projects are usually structured in Ruby. You DO require all the components of the library within the files. Using some mechanism to automatically require all the files individually (and separately) wouldn't make a whole lot of sense because when it comes time to actually require the library in some other code (like an application) you'd only be able to use the Ruby built-in code loading features, like `require`. So, if you structure your library like this, you simply `require 'lib'` and all the other files are loaded up as part of the top-level module. – Dave N Dec 06 '14 at 19:13
  • OK, thanks! Seems like my question was a bit convoluted but the solution is simply requiring everything. The neat little trick of making a require "gateway" is nice. – moshewe Dec 06 '14 at 20:04