Search code examples
rubyrequire

Can you require multiple packages on the same line?


I have this in my code:

require 'uri'
require 'net/http'
require 'csv'

Which isn't super compact or DRY. Can you put multiple require's on the same line?

Something like this:

require 'uri', 'net/http', 'csv'

Solution

  • I don't think so. Reading the Kernel#require it only accepts one filename as argument.

    But you can do something like (don't know if this will feel you better).

    %w[uri net/http csv].each { |f| require f }
    

    It's common to perform a runtime require for relative paths like reading files from folder for example.