Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does rubocop's Rails/FilePath cop recommend Rails.root.join

Tags:

ruby

rubocop

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.

like image 873
CodeSmith Avatar asked Dec 29 '17 18:12

CodeSmith


3 Answers

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.

like image 200
CodeSmith Avatar answered Nov 12 '22 22:11

CodeSmith


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.

like image 35
Axe Avatar answered Nov 12 '22 23:11

Axe


I think it's just a "let's pick a style" approach. I don't see any meaningful discussion when the idea was proposed.

like image 2
Mickey Sheu Avatar answered Nov 12 '22 23:11

Mickey Sheu