Skip to content

Instantly share code, notes, and snippets.

@armw4
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armw4/8879438 to your computer and use it in GitHub Desktop.
Save armw4/8879438 to your computer and use it in GitHub Desktop.
What happens when you invoke File.join with variable arguments?

by Antwan Wimberly

I finally figured it out (probably could have just ready the documentation, but who has time for that when they're gluttonously binging on source code)?

"You're eating right now, aren't you? You disgust us..."

It's just a platform independant way of creating a path. It's abstracting the proper separator for you (which is either a backslash or forward flash depending on whether or not you're on *nix or Windows). It's even smart enough to not add a slash to the path if any of your arguments already contain it.

File.join("hi-there/now", 'locales', '*.yml') # => "hi-there/now/locales/*.yml"
 
File.join('hi-there', '/now', 'locales', '*.yml') # => "hi-there/now/locales/*.yml"

File.join('hi-there', '/now', 'locales', '//*.yml') # => "hi-there/now/locales//*.yml"

File.join('hi-there', '/now', '/locales', '*.yml') # => "hi-there/now/locales/*.yml" 

Again, it may seem a bit petty, but if you want the right separator for your target platform, then this is the way to go. You'll find an equivalent API over in node.js land.....likely inspired by ruby (cough, cough).

@armw4
Copy link
Author

armw4 commented Feb 8, 2014

Keep in mind that this is just an in memory string manipulation function. It does not actually verify that the resulting path exists. It's sole purpose is to return a string. File.join _in no way_ interacts with your file system. So don't be scared to throw it a few curve balls en route of testing it's tolerance level (in the event you have a mind as curious of that which belongs to me).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment