Why do some Ruby projects have a "require" file

I've looked at different Ruby client implementations, and sometimes I'll find files like this :

require 'addressable/uri'
require 'twitter/configuration'
require 'twitter/cursor'
require 'twitter/direct_message'
require 'twitter/entity'
require 'twitter/entity/hashtag'
# ... Snipped for brevity; there 31 lines like this.

      

which do nothing but require

other files. Why is this being done?

+3


source to share


2 answers


It's just a wrapper that calls other requirements, so one script user won't require all dependencies. Sometimes a package would rather divide its functions into different files than put all of its code in one file. It also makes it easier to make changes and track bugs. Also, committing changes to the repository can be easier.



+3


source


Ruby Gems and Libraries Frequently Consolidate Requirement Statements

This is a fairly common idiom in Ruby library files. For example, consider the following template generated by the bundler gem command :

$ bundler gem foo
$ cat foo/lib/foo.rb
require "foo/version"

module Foo
  # Your code goes here...
end

      



The bundle template expects you to populate this file with various query statements to pull code out of the foo / lib / foo / directory. For example:

require "foo/version"

module Foo
  require 'foo/bar' # pull in lib/foo/bar.rb
  require 'foo/baz' # pull in lib/foo/baz.rb
end

      

You can put 100% of your code in lib / foo.rb if you like. However, splitting the files so that each one focuses on a separate issue is often considered good programming practice, and the main library file is a very convenient place to consolidate a lot of Core # requires .

+1


source







All Articles