The following fails with Errno::ENOENT: No such file or directory
, even if the file exists:
open('~/some_file')
However, I can do this:
open(File.expand_path('~/some_file'))
I have two questions:
open
process the tilde as pointing to the home directory?File.expand_path
?$HOME
is a mere convention; indeed, if you look at the documentation for File.expand_path
, it correctly interprets the tilde, but it's a feature of the function itself, not something inherent to the underlying system; also, File.expand_path
requires the $HOME
environment variable to be correctly set. Which bring us to the possible alternative...Try this:
open(ENV['HOME']+'/some_file')
I hope it's slick enough. I personally think using an environment variable is semantically clearer than using expand_path
.