Short answer
It sounds like you're writing a short script. If that's the case, there's no reason you can't use a modifiable global variable like $tmp_dir. But if you don't need to modify it, you should use @sawa's solution of a global constant like TMP_DIR. In that case you should call .freeze on the string to avoid accidental modification.
Longer answer
If the script becomes longer or more complex, you should refactor into classes. The TMP_DIR solution will still work in this case. But if you need to modify the value, you can create a ConfigObject class to group these variables.
Example:
ConfigObject = Struct.new(:tmp_dir, :file_limit)
# it's a good idea to create this before everything else
$config = ConfigObject.new('/tmp/dir', 10)
class Foo
def do_something
$config.file_limit # use this somehow
$config.file_limit = 5 # change
end
end
A similar technique uses class variables to accomplish the same thing:
class ConfigObject
class << self
attr_accessor :tmp_dir, :file_limit
end
@tmp_dir = '/tmp/dir'
@file_limit = 10
end
class Foo
def do_something
ConfigObject.file_limit # use this somehow
ConfigObject.file_limit = 5 # change
end
end
Think of ConfigObject as a "service" used by other classes. If your app gets complicated enough to need multiple interacting services, you might need want to set up a kind of service registry that hold references to the services (google "dependency injection" for more info).
Note: you shouldn't name your class Config because that's already a built-in class.