Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is File.join useful?

Tags:

ruby

From reading the documentation, it's apparent that File.join joins the given parameters with the / character.
When is using this, as opposed to filenames.join('/'), beneficial?

like image 412
Fahad Sadah Avatar asked Nov 06 '10 18:11

Fahad Sadah


2 Answers

There is another, subtle difference:

File.join('foo','bar') #=> "foo/bar" ['foo','bar'].join('/') #=> "foo/bar" 

But, if you pass an argument already ending with / (which is quite often when working with paths), you won't have two slashes in the result:

File.join('foo/','bar') #=> "foo/bar" ['foo/','bar'].join('/') #=> "foo//bar" 
like image 157
Mladen Jablanović Avatar answered Oct 04 '22 08:10

Mladen Jablanović


It will use File::SEPARATOR, which in theory need not be /.

like image 36
Matthew Flaschen Avatar answered Oct 04 '22 09:10

Matthew Flaschen