I wish someone would enlighten me. I cannot for the life of me figure out why we use YML in Ruby. This is not a YML vs XML vs JSON discussion either. It’s a question of why we would use any external configuration file.
External configuration files, as far as I can tell, began because of compiled languages. Back in the day you would compile entire systems down into a jar file, exe, dll or some such file. There would always be those little bits of information though that you could not possibly know. What is the exact path to a particular folder for images? What is the database server address?
Here we are in Rails (or at least those of us that have come over) years later doing the same thing. The funny thing is though, we are doing it half way.
We are in a dynamic, not compiled language. We are using one of the most expressive languages out there. We are don’t have a reason (as far as I can tell) to need a standalone configuration file in another language.
I’ve heard the argument that they are easy to find. If you structure your application correctly, no matter if it’s code or configuration settings, finding them should be obvious (config folders, etc.).
I’m picking on the project a little here, but what got to me was when I saw an example of Mongoid and I saw this:
production:
<<: *defaults
host: <%= ENV['MONGOID_HOST'] %>
port: <%= ENV['MONGOID_PORT'] %>
database: <%= ENV['MONGOID_DATABASE'] %>
So now we are mixing ERB with YML? I’m sorry, but this in what I call a code smell. Why are we not just using Ruby?
I agree with your wholeheartedly, Joe. YAML just doesn’t add any value to our day-to-day lives living and working with Ruby. If anything it adds a pointless and wasteful dependency to turn the data structure you’ve represented in your config.yml file into something Ruby can understand.
I would expect mature projects like Rails and RubyGems to be moving entirely away from YAML and use Ruby for expressing what they’re trying to model – not even in a DSL (which someone mistook my meaning for the last time I talked along similar lines) but in bog standard, honest to goodness Ruby.
If you mean a Hash, write one. Don’t pussy-foot around it with YAML in a config/database.yml file. If the people building web applications using Rails can’t handle a simple Ruby data structure then something is fundamentally flawed – give people more credit. It’s a smell, it’s not required and it’s a (very small, true) barrier to entry.
Joe, I’ve been asking that same question since… I don’t know when. In fact, one day I had a minor meltdown over our team’s use of YML in Ruby; case in point: http://laribee.com/configuration-come-to-jesus
YAML is easy to read and easy to parse. At this point in the game, I expect a Rails app to have some YAML and I expect that the production, pre-production, staging, and ci servers will all safely know what they need to know and github will not know what they need not know.
Would it all work in .rb files? Sure. At this point, I’ll lean toward the principle of least surprise for the next new dev on a project.
I too am not a YAML fan (and have also found myself using and being frustrated with ERB in a Rails YAML config file).
The only reason I can really think of is that it’s easy to use in multiple languages. Other than that, Ryan’s right – in Ruby only projects it just adds a dependency and another (annoying and inflexible) layer.
When I read this, I began thinking about database.yml versus routes.rb (especially in earlier versions of Rails) and why one should be in markup and the other Ruby. No question that routing is more complex, but it’s still essentially declarative. So why the difference?
Pingback: Why Not YAML? « Baroquebobcat
Pingback: Why Yml?