Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Yet Another Configuration Helper Tool
tree: cd68240c67

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.



Yacht is an application configuration gem that lets you define settings for multiple environments in YAML files. It is similar to AppConfig with additional features like:

  • use of ClassyStruct for improved performance over OpenStruct

  • protection of sensitive settings by specifying a whitelist in a YAML file

  • easy override of nested keys (not pretty with YAML references)

  • no need for an initializer or constant to store loaded values (just use Yacht.my_key)


  • Rails: Add this to your Gemfile and run the bundle command.

    gem "yacht"
  • Outside of rails, just require the gem as usual:

    require 'rubygems'
    require 'yacht'

Getting Started

Step 1: YAML files

First create one or more of the following YAML files in the same directory to define your settings:

# config/yacht/base.yml (required)
    copyright_year: 2011
    company_name: AT&T Interactive
    aws_key: foofoo
    twitter_key: barbar
  cdn_host: localhost
    # you can safely overwrite a single value in a nested key
    # YAML references (& and *) don't let you do this
    #   see for an explanation
    aws_key: bazbaz

# config/yacht/whitelist.yml (optional)
# any keys specified here can be used as a whitelist filter:
#   Yacht::Loader.to_hash(:apply_whitelist? => true)
#   or
#   Yacht::Loader.to_classy_struct(:apply_whitelist? => true)
#   (by default the whitelist is ignored)
# NOTE: the whitelist is ignored when using Yacht.my_key or Yacht['my_key']
#       you have to use Yacht::Loader#to_hash or
#       Yacht::Loader#to_classy_struct to use the whitelist
- public_info

# config/yacht/local.yml (optional)
# any values set in local.yml will override values set in base.yml
# useful for development and testing
cdn_host: localhost

Step 2: Use Yacht.my_key or Yacht['my_key'] in ruby

  • Rails:

    # now you can access any key set in your YAML files with:
    # => "my_value"
    # => "my_value"
  • Outside of rails, you need to tell Yacht where your YAML files are stored, and what environment you want to use.

    Yacht::Loader.dir          = '/path/to/YAML/dir'
    Yacht::Loader.environment  = 'my_environment'

Other features

Yacht::Loader.to_js_snippet export to javascript

If you would like to access values stored in Yacht inside of javascript, there is a helper for that. First, create a YAML file to tell Yacht which keys should be exported:

# config/yacht/js_keys.yml
# only keys listed here will be available in javascript
# remember that any values exported to javascript will be visible to all visitors to your site
- cookie_domain

Then use Yacht::Loader#to_js_snippet to create a string that can be eval'd or included in the DOM:

# => ";var Yacht = {\"cookie_domain\":\"\"};"

You can also add in extra values from outside of Yacht using the :merge option, like so:

Yacht::Loader.to_js_snippet(:merge => {:current_time =>})
# => ";var Yacht = {\"cookie_domain\":\"\",\"current_time\":\"06/29/2011\"};"

Rails support

When used inside of a Rails application, Yacht offers some convenience features:

  1. Yacht.environment will be set by default to the currently loaded environment. For example, if you start your application in test mode, Yacht's values will be determined by the test entry in base.yml .

  2. Yacht.dir will be set to /RAILS_ROOT/config/yacht (where RAILS_ROOT is the location of your Rails app).

  3. The yacht_js_snippet Rails helper wraps the string from Yacht::Loader#to_js_snippet in a script tag using Rails' javascript_tag helper.

    # => "development"  # Yacht automatically adds the current Rails environment in ruby
    # If you want rails_env included in javascript, just add it to js_keys.yml
    # inside a view or helper:
    # => "<script type=\"text/javascript\">\n//<![CDATA[\n;var Yacht = {\"cookie_domain\":\"localhost\",\"rails_env\":\"development\"};\n//]]>\n</script>"
    # you can also pass options to yacht_js_snippet:
    yacht_js_snippet(:merge => {:current_time =>})
    # => "<script type=\"text/javascript\">\n//<![CDATA[\n;var Yacht = {\"cookie_domain\":\"localhost\",\"rails_env\":\"development\",\"current_time\":\"06/29/2011\"};\n//]]>\n</script>"
  4. Yacht.rails_env will be defined and match the currently loaded Rails environment.

Ruby compatibility

Yacht works with ruby 1.8.7 and 1.9.2.


Yacht is licensed under the MIT License.

Something went wrong with that request. Please try again.