Reputation: 149
I am making a command line tool and I am using yaml to make a config file. But right now I can only access the tool when I am in the same directory as that of myprogram.yml
.
private
CONFIG_FILE = 'myprogram.yml'
def write_config
config = {}
config['username']=@username
config['password']=@password
File.open(CONFIG_FILE, 'w') do |f|
f.write config.to_yaml
end
end
def read_config
config = YAML.load_file(CONFIG_FILE)
@username = config['username']
@password = config['password']
end
How can I make this file to be accessed from any directory on my computer?
Upvotes: 0
Views: 176
Reputation: 13354
You'll want to give the absolute directory of the myprogram.yml
file. Right now it will look in the directory where you are executing the ruby script from. By making it absolute, the script can run anywhere and know where to find the config file.
Example:
private
CONFIG_FILE = '/Users/myuser/config/myprogram.yml'
def write_config
config = {}
config['username']=@username
config['password']=@password
File.open(CONFIG_FILE, 'w') do |f|
f.write config.to_yaml
end
end
def read_config
config = YAML.load_file(CONFIG_FILE)
@username = config['username']
@password = config['password']
end
Upvotes: 1