Yaml config files are a great way to store static data in your rails application. But there’s a little caveat I experienced today. Observe the following yaml file:
dog: poodle husky "golden retriever"
Looks fine, right? Now let’s assume you’ve named this yaml file dog.yml and it lives in your application’s config directory. You’d figure you could access the data by doing something like the following:
config=YAML.load_file('dog.yml') puts config (yields the hash {"dog" => { "poodle", "husky", "golden retriever" } }
But for whatever reason, you are getting an error (couldn’t parse YAML at line x column x). You suspected that “golden retriever” might be a problem due to the space, but you’ve already quoted it, so no, it can’t be the problem.
I spun my wheels for at least an hour on a similar problem today, until I finally realized that my values (poodle, husky, ect..) where tabbed, as opposed to moved over via spaces. Yaml does not allow this.
So if when debugging your yaml files you receive an error you just can’t quite pinpoint, lookout for that pesky tab!
For more about yaml, check out this wiki.
Nice article! What was the error message you encountered? Add the error message to the article so searchers will find your article – and the solution.
Thanks Yarnov! I’ve added the error message in bold to the post.