Ed de Almeida Ed de Almeida - 1 year ago 79
Ruby Question

Require order in a Ruby gem

The Ruby gem I'm updating with new features, named politicosbr, depends on four other Ruby gems and requires them in its main module.

require 'open-uri'
require 'nokogiri'
require 'httparty'
require 'roo-xls'

But it also requires some internal files:

require 'politicosbr/constantes'
require 'politicosbr/estaduais'
require 'politicosbr/vereadores'

It's just a matter of style, probably, but anyway I would like to know if there is any Ruby convention about the order of the requires in this case.

In other words, should I require the internal files first, or the other gems first, or does it not matter at all?

Answer Source

You should require the external files before the internal files because the external files should not depend on your files, but your internal files depend on the external files.

For example, suppose an external file defines class A as a subclass of B, and you are using class B in your files. If you simply (as usual) do in your internal file:

class B

then that will create class B as the direct subclass of Object. If you later include the file that defines B as a subclass of A, then there will be a contradiction at that point.