Getting user home directory
-
As I google for a solution it seems that people use either
ENV['HOME']
orFile.expand_path('~')
.Neither of these work on PC since it looks like the variable is HOMEPATH instead. So I was just hoping for a sanity check and wondering what others are doing here. Will this be reliable?
if (WIN) userPath = ENV['HOMEDRIVE'] + ENV['HOMEPATH'] elsif (MAC) userPath = File.expand_path('~') end
-
See my code example:
[ Code ] !autoload.rb loads "!_autoload" foldersand FYI:
@unknownuser said:
](http://www.ruby-doc.org/core-1.8.6/classes/File.html#M000794)":1laxg9xj]
File.expand_path(file_name [, dir_string] ) → abs_file_name
Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point. The given pathname may start with a
**` ~`**’’, which expands to the process owner‘s home directory (**%(#008000)[the environment variable HOME must be set correctly]**.)
~user
’’ expands to the named user‘s home directory. -
Surely
ENV["USERPROFILE"]
gives it in one step ? -
As my code does:
ENV['HOME']=ENV['USERPROFILE'] unless ENV['HOME'] ENV['USER']=ENV['USERNAME'] unless ENV['USER'] # now set a global; $HOME = ENV['HOME'] # .. or use the tilde shortcut within pathstrings.
BTW.. the "
~*user*
" feature does not work on Windows. Likely because of Security issues.
Advertisement