Easy Configuration with Ruby and Yaml

written by Jerod on August 19th, 2008 @ 02:34 PM

Even trivial apps need to be configured. I used to simply define my app config somewhere near the top of the file, as many others do.

However, this becomes troublesome in a few common scenarios:

  1. You want to share your source code with somebody else, but not your super-secret password
  2. Your application becomes more complex and multiple areas need access to configuration variables

Abstracting configuration out of your Ruby app and into a separate Yaml file is super-simple. Here’s some codey code to use as an example:

   1  # this is 'myapp.rb'
   2  require 'yaml'
   3  CONFIG = Yaml.load_file("config.yml") unless defined? CONFIG
   4  
   5  puts "Your super-secret password is #{CONFIG['password']}"

Can it get any easier than that? I submit that it, in fact, cannot get any easier. You probably want to know what the config.yml file looks like, huh:

   1  # this is my 'config.yml'
   2  username: sant0sk1
   3  password: awesome

Now if you want to share your source with a friend, perhaps via git, you can just add config.yml to the .gitignore file in your repository and create a config-sample.yml which holds dummy values.

Any questions?

Post a comment

Options:

Size

Colors

Clicky Web Analytics