Q:

Ruby: How to import a variable from another file?

I'm trying to create a local config file to use with compass so we can cope with differing import paths on developers' machines. So far I've tried to import the file inside an exception block, incase it doesn't exist, then use the variable further down:
local_config.rb
VENV_FOLDER = 'venv'

config.rb
VENV_FOLDER = '.'
begin
  require 'local_config.rb'
rescue LoadError
end
puts VENV_FOLDER

Normally I'm a Python developer so I'd expect the import to change the value of VENV_FOLDER to venv, however it's still . afterwards.
Is there a way to import local_config.rb in such a way that it overrides the value of VENV_FOLDER?

A:

Other alternatives:
YAML (or JSON)
local_config.yml
venv_folder: 'venv'

config.rb
require 'yaml'

VENV_FOLDER = begin
  YAML.load_file('local_config.yml').fetch('venv_folder')
rescue Errno::ENOENT, KeyError
  '.'
end

puts VENV_FOLDER

Class instance variable
You could put the value in a class instance variable:
local_config.rb
Config.venv = 'venv'

config.rb
class Config
  class << self ; attr_accessor :venv ; end
  self.venv = '.'
end

begin
  require './local_config.rb'
rescue LoadError
end

puts Config.venv

Constants
Also, sticking to ruby files with constants, the following is perhaps marginally clearer in its intentions and avoids having to catch exceptions.
local_config.rb
VENV_FOLDER = 'venv'

config.rb
config_file = './local_config.rb'
require config_file if File.file? config_file
VENV_FOLDER ||= '.'

puts VENV_FOLDER

All three solutions have different mechanisms for ensuring that the value will be set even if the file is missing or doesn't set the value as expected. Hope it's helpful

