moshewe
moshewe

Reputation: 127

`require`ing files in a common codebase

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:

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?

Upvotes: 1

Views: 55

Answers (1)

Dave N
Dave N

Reputation: 398

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

Upvotes: 2

Related Questions