在Ruby中获取用户主目录的跨平台方法? [英] Cross-platform means of getting user's home directory in Ruby?
问题描述
Java具有方便的 System.getProperty( user.home)
来以独立于平台的方式获取用户的 home目录。 Ruby相当于什么?我没有Windows盒子可以玩,而且我觉得依靠文件名中的波浪号不是最干净的方法。有其他选择吗?
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?
推荐答案
File.expand_path
方法使用Unix惯例来处理波浪号(〜
),因此〜
引用当前用户的主目录,而〜foo
引用到 foo
的主目录。
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.
我不知道是否有更好或更惯用的方式,但是 File.expand_path('〜')
应该可以帮助您。
I don't know if there's a better or more idiomatic way, but File.expand_path('~')
should get you going.
这篇关于在Ruby中获取用户主目录的跨平台方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!