Reputation: 243
I have 2 files which should be run in order. I create run.rb file:
files =[
'./file-name-1.rb',
'./file-name-2.rb',
]
files.each do |file|
require file
end
And run it. Are there more correct methods of solving this problem?
Upvotes: 0
Views: 48
Reputation: 106027
Since it is file-name-2.rb
that depends on file-name-1.rb
, then it should be the one to require "file-name-1.rb"
. It should not be run.rb
's job to know what another file's dependencies are. The correct way to solve this, then, is:
require "file-name-1.rb"
# ...
require "file-name-2.rb
# ....
And, as Frederick suggests in the comments above, it's unorthodox for a file to do work other than declaring constants (classes, modules) and/or methods at the time it's required. Instead, it should define methods to do that work, and then other files that require
it can invoke those methods. This way you always know exactly when the work will be done, even if your application has a complex dependency structure.
Upvotes: 1