hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

YAML's Merge Key #

by why in bits

Some of you might find YAML’s merge key convenient, especially in your database.yml files within Rails. The merge key basically mixes two hashes together, just like the Hash#merge method. You just place a << key in a hash with a value which is a hash or an array of hashes.

 death masks are?:
   sad: 2
   <<: {magnificent: 4}

Is just like:

 death masks are?:
   sad: 2
   magnificent: 4

So, if you are using the same login for different databases (which is likely very common):

 login: &login
   adapter: mysql
   host: localhost
   username: portly
   password: 666HELL666

 development:
   database: rails_dev
   <<: *login

 test:
   database: rails_test
   <<: *login

 production:
   database: rails_prod
   <<: *login

The login anchor is set up first and then its anchor is used throughout. I don’t think having a login entry in the file does any harm, does it?

said on 26 Jul 2005 at 10:52

My, that is handy.

Is there an equivalent of that for string interpolation?

said on 26 Jul 2005 at 11:22

Well, there’s yip.

said on 26 Jul 2005 at 12:15

Very clever indeed. Is there any way to make it span files?

said on 26 Jul 2005 at 12:21

I love Ruby. Seriously.

<MenTaLguY> Is there a #{thingy} for #{stuff I want to do}?

<Ruby master> Well, there’s #{rubyforge project}.

said on 26 Jul 2005 at 14:47

There was just a checkin to the Rails tree to allow ERb in the database.yml file.

said on 26 Jul 2005 at 14:56

666HELL666? Hee!

said on 27 Jul 2005 at 01:44

It’s neat to see yip mentioned after so long a time.

Please note that yip is available as a rubygem. You can reach me at wbruce (gmail) if you any problems.

said on 25 Aug 2005 at 08:02

Just make sure you use quotes when you use slashes like so:

socket: ”/tmp/mysql.sock”

Comments are closed for this entry.