In Ruby, you can create configuration files using a YAML (YAML Ain’t Markup Language) file. Here’s an example of how to create and use a YAML config file in a Ruby application:
- Create a YAML file with the configuration data, for example
config.yml
:
development:
host: localhost
port: 3000
production:
host: example.com
port: 80
- Load the YAML file in your Ruby code:
require 'yaml'
config = YAML.load_file('config.yml')
# access the values in the config file
host = config[ENV['RAILS_ENV']]['host']
port = config[ENV['RAILS_ENV']]['port']
- Use the values in your code:
puts "Host: #{host}"
puts "Port: #{port}"
Note that in this example, the environment (development or production) is determined by the RAILS_ENV
environment variable. You can use other methods to set the environment if desired.