Please, Ruby devs, join() your paths
Like in most programming languages, when you write paths in ruby, e.g. to open a file you pass in a string:
This is a serious smell for several reasons. Not, as people often believe, just to cater the few Ruby developers on Windows (Windows knows how to follow “/foo/bar/” paths just as well as “\foo\bar” nowadays).
But mostly because this does not scale, gets convoluted real quick. Like so:
Ruby offers a great File.join()
class method, for this. This simply uses the File::SEPARATOR
to join a string.
As you may notice, double slashes are eliminated.
Also, you can inherit this behaviour from Pathname
, like Rails.root
does.
Rolling your own, is very beneficial, and simple too.
There really is no reason to fiddle with strings, concatenate slashes and whatnot, to build paths. Join is so much easier, more powerfull and above all, cleaner and more portable.