Get Available Environments From YML File

I am creating a GUI for handling rails migration files and need to check
the list of defined environments in the database.yml file. Not being an
expert in reading YML files in ruby, I’m not sure what the best way to
do this is. I assume that I need to make some call to a YML parser but
I have no clue where to start. Anyone know how to do this?

Example:
development:
adapter: mysql
database: development
password: mypasswrd
host: localhost
port: 3306

test:
adapter: mysql
database: test
password: mypasswrd
host: localhost
port: 3306

production:
adapter: mysql
database: production
password: mypasswrd
host: localhost
port: 3306

…would return [“development”, “test”, “production”].

Wow! That was alot easier than I thought. Here’s a one-liner to get a
list of defined environments in your database.yml file:

YAML::load(File.open("#{RAILS_ROOT}/config/database.yml")).keys

That’s it.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs