I have the following line in my code:
require "#{Rails.root}/config/environments/production.rb"
Rubocop's Rails/FilePath
cop recommends I change it to:
require Rails.root.join('config', 'environments', 'production.rb')
Why? The former is more compact, appears faster and is arguably more readable.
As other's have mentioned, RuboCop recommends doing this to avoid problems with Operating Systems that don't use /
as the path separator. The RuboCop team added some clarification after this question was asked (as seen at https://github.com/bbatsov/rubocop/pull/5571/files).
That being said, I'm not sure this cop is necessary. From other SO threads, like slash and backslash in Ruby, it sounds like Ruby does a good job of automatically converting paths behind the scenes (when code is executing in Ruby). In the cases where paths are being passed to external systems, people will probably be very specific about the path structure.
From the Rubocop manual
This cop is used to identify usages of file path joining process to use Rails.root.join clause. This is to avoid bugs on operating system that don't use '/' as the path separator.
I think it's just a "let's pick a style" approach. I don't see any meaningful discussion when the idea was proposed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With