If I do:
require \'inifile\'
# read an existing file
file = IniFile.load(\'~/.config\')
data = file[\'profile\'] # error here
puts data[\'region\']
Ruby has a method for this case. It is File::expand_path.
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 (the environment variable HOME must be set correctly).
“~user”
expands to the named user’shome
directory.
require 'inifile'
# read an existing file
file = IniFile.load(File.expand_path('~/.config'))
When given ~
in a path at the command line, the shell converts ~
to the user's home directory. Ruby doesn't do that.
You could replace ~
using something like:
'~/.config'.sub('~', ENV['HOME'])
=> "/Users/ttm/.config"
or just reference the file as:
File.join(ENV['HOME'], '.config')
=> "/Users/ttm/.config"
or:
File.realpath('.config', ENV['HOME'])
=> "/Users/ttm/.config"