Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Nov 29 ’10

Use OpenStruct for application configuration variables

Every rails app I’ve ever built needs some sort of configuration, and I seem to be solving this problem a different way every time, which really bothers me. Today I learned about a new class called OpenStruct. Here’s how you could use it.

# in app_root/config/initializers/app_config.rb

require 'ostruct'

AppConfig = OpenStruct.new

AppConfig.default_email = "no-reply@example.com"
AppConfig.api_url = "staging.someapi.com"

Now you have a neat way of defining global configuration variables without using constants or defining custom classes.

This tip was submitted by Jon Druse.

10 notes 0 comments

  1. atm09td reblogged this from rubyquicktips
  2. Jon Druse submitted this to rubyquicktips

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus