Monday, April 22, 2013

Load hash from YAML with keys as symbols in Ruby

The most easy way is to start keys with colon ':'

:db:
  - :host: db.host.com
    :port: 5210

Now when you load this

db_config = YAML::load(File.open(File.expand_path('../config/database.yml', File.dirname(__FILE__))))

it will be "symbolized"

2 comments:

  1. Awesome thank you! This lets me use YAML to work with the new double splat in ruby 2.0

    ReplyDelete