From: Jeremy Bopp Date: 2011-06-18T07:37:54+09:00 Subject: Re: default config file location On 6/17/2011 14:52, Michael Edgar wrote: > On Jun 17, 2011, at 3:49 PM, Jeremy Bopp wrote: >> >> According to the documentation for File.expand_path, all that is >> required is that $HOME is set correctly. It doesn't apparently use the >> shell to figure this out. Chad's method even works on Windows. :-) >> >> I might simplify this just a tiny bit though: >> >> File.expand_path("~/#{filename}") > > I use File.join for such situations due to separator differences. Is this > unnecessary for some reason? Ruby always uses '/' with File.join wherever I've tested it, even on Windows. Fortunately, Windows will also accept that separator, and Ruby on Windows will work with either just like other Windows applications. However, it's possible that one day Ruby will be ported to an OS that doesn't support it. Using File.join or something equivalent (see below) should pretty much guarantee that your script won't trip over that problem. I actually had the very problem you're concerned about in a large Perl framework years ago that had to run on Windows, Unix, and MacOS 9. For those too young to remember, MacOS 9 used ':' for the file separator character, among other path-related differences. Since then, I tend to use methods like File.join for long-lived or important projects. The only real problem at the moment in Ruby is displaying Ruby-style paths to a Windows user. Since they're used to seeing the '\' separator, many may not understand the '/' or at least be uncomfortable using it. In such cases where you need to show a path that was generated internally to a user, it might be a good idea to use a platform-aware filter to make the path more presentable/palatable. BTW, another way to handle this separator issue is to use the Pathname class from the pathname library for all your paths: require 'pathname' puts (Pathname.new('~') + filename).expand_path -Jeremy