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

My, that is handy.

Is there an equivalent of that for string interpolation?

said on

Well, there’s yip.

said on

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

said on

I love Ruby. Seriously.

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

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

said on

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

said on

666HELL666? Hee!

said on

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

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

socket: ”/tmp/mysql.sock”

Comments are closed for this entry.