在Ruby中获取用户主目录的跨平台方法?

时间:2021-07-21 12:21:06

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?

Java有convienient System.getProperty(“user.home”)以独立于平台的方式获取用户的“home”目录。Ruby中的等效项是什么?我没有Windows框,我觉得在文件名中使用斜线不是最干净的方式。有替代品吗?

4 个解决方案

#1


77  

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.

该文件。expand_path方法使用Unix约定专门处理tilde(~),因此~引用当前用户的主目录,~foo引用foo的主目录。

I don't know if there's a better or more idiomatic way, but File.expand_path('~') should get you going.

我不知道是否有更好或更习惯的方法,但是File.expand_path('~')应该能让您走。

#2


87  

With Ruby 1.9 and above you can use Dir.home.

使用Ruby 1.9或以上版本,您可以使用Dir.home。

#3


10  

ENV["HOME"] or ENV["HOMEPATH"] should give you what you want.

ENV[HOME]或ENV["主页"]应该给你你想要的。

homes = ["HOME", "HOMEPATH"]

realHome = homes.detect {|h| ENV[h] != nil}

if not realHome
   puts "Could not find home directory"
end

#4


8  

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:

在unix平台(linux、OS X等)、ENV["HOME"]、File.expandpath('~')或Dir。home都依赖于正在设置的home环境变量,但有时您会发现没有设置环境变量——这在运行启动脚本或批处理调度器时很常见。如果您处于这种情况,您仍然可以通过以下方式获得正确的主目录:

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.

话虽如此,由于这个问题需要一个“跨平台”方法,所以必须注意,这在Windows上是行不通的(getpwuid将返回nil)。在Windows上,ENV[“HOME”]和上面提到的依赖于它的方法将会起作用,尽管HOME变量通常不在Windows上设置——在启动时,Ruby将基于Windows主页和HOMEDRIVE环境变量填充ENV[“HOME”]。如果windows HOMEDRIVE和主页环境变量没有设置,那么这将不起作用。我不知道这在Windows环境中有多普遍,我也不知道在Windows上还有什么其他的选择。

#1


77  

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.

该文件。expand_path方法使用Unix约定专门处理tilde(~),因此~引用当前用户的主目录,~foo引用foo的主目录。

I don't know if there's a better or more idiomatic way, but File.expand_path('~') should get you going.

我不知道是否有更好或更习惯的方法,但是File.expand_path('~')应该能让您走。

#2


87  

With Ruby 1.9 and above you can use Dir.home.

使用Ruby 1.9或以上版本,您可以使用Dir.home。

#3


10  

ENV["HOME"] or ENV["HOMEPATH"] should give you what you want.

ENV[HOME]或ENV["主页"]应该给你你想要的。

homes = ["HOME", "HOMEPATH"]

realHome = homes.detect {|h| ENV[h] != nil}

if not realHome
   puts "Could not find home directory"
end

#4


8  

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:

在unix平台(linux、OS X等)、ENV["HOME"]、File.expandpath('~')或Dir。home都依赖于正在设置的home环境变量,但有时您会发现没有设置环境变量——这在运行启动脚本或批处理调度器时很常见。如果您处于这种情况,您仍然可以通过以下方式获得正确的主目录:

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.

话虽如此,由于这个问题需要一个“跨平台”方法,所以必须注意,这在Windows上是行不通的(getpwuid将返回nil)。在Windows上,ENV[“HOME”]和上面提到的依赖于它的方法将会起作用,尽管HOME变量通常不在Windows上设置——在启动时,Ruby将基于Windows主页和HOMEDRIVE环境变量填充ENV[“HOME”]。如果windows HOMEDRIVE和主页环境变量没有设置,那么这将不起作用。我不知道这在Windows环境中有多普遍,我也不知道在Windows上还有什么其他的选择。