Cross-platform means of getting user's home directory in Ruby?
Solution 1
The File.expand_path
method uses the Unix convention of treating the tilde (~
) specially, so that ~
refers to the current user's home directory and ~foo
refers to foo
's home directory.
I don't know if there's a better or more idiomatic way, but File.expand_path('~')
should get you going.
Solution 2
With Ruby 1.9 and above you can use Dir.home
.
Solution 3
This works on all operating systems
- For the current user
Dir.home
- For a given user
Dir.home('username')
Note: Username is case sensitive on Linux but not on Windows or macOS
Solution 4
ENV["HOME"]
or ENV["HOMEPATH"]
should give you what you want.
homes = ["HOME", "HOMEPATH"]
realHome = homes.detect {|h| ENV[h] != nil}
if not realHome
puts "Could not find home directory"
end
Solution 5
On unix platforms (linux, OS X, etc), ENV["HOME"]
, File.expandpath('~')
or Dir.home
all rely on the HOME
environment variable being set. But sometimes you'll find that the environment variable isn't set--this is common if you're running from a startup script, or from some batch schedulers. If you're in this situation, you can still get your correct home directory via the following:
require 'etc'
Etc.getpwuid.dir
Having said that, since this question is asking for a "cross-platform" method it must be noted that this won't work on Windows (Etc.getpwuid
will return nil
there.) On Windows, ENV["HOME"]
and the methods mentioned above that rely on it will work, despite the HOME
variable not being commonly set on Windows--at startup, Ruby will fill in ENV["HOME"]
based on the windows HOMEPATH
and HOMEDRIVE
environment variables. If the windows HOMEDRIVE
and HOMEPATH
environment variables aren't set then this won't work. I don't know how common that actually is in Windows environments, and I don't know of any alternative that works on Windows.
Comments
-
davetron5000 almost 2 years
Java has the convienient
System.getProperty("user.home")
to get the user's "home" directory in a platform-independent way. What's the equivalent in Ruby? I don't have a Windows box to play around with, and I feel like relying on tildes in filenames isn't the cleanest way. Are there alternatives? -
davetron5000 over 13 yearsis that true on Windows, tho?
-
davetron5000 over 13 yearsis that true on Windows, tho? The docs don't indicate as such
-
Jacob Relkin over 13 years@davetron5000 That's what my code is doing,
HOMEPATH
is an environment variable only on Windows. -
davetron5000 over 13 yearsSo, essentially we have to roll it ourselves, there's no system call to get it? That is strangely lame for Ruby…
-
davetron5000 about 13 yearsEverything on this page works (at least on Windows 7), but I think Dir.home and using tilde seem the best.
-
ckruse over 8 yearsThis is the correct answer. Thanks for the pointer to the etc module.
-
taylorthurlow about 6 yearsAdditionally,
Dir.home('my_username')
will give you the home directory of a specific user. -
ankostis about 5 years
HOMEPATH
on Windows does not include the drive-letter - so won't work if user-folder resides inD:
.