Which way is the correct way to pass variables to chef templates?

Solution 1:

This is a ruby thing, not a chef thing.

When you use ( :key => value ) you are passing in an implied hash. Ruby decides that what it sees inside the parens is hash-like, and converts to a hash automatically.

When you use { }, you are actually passing a block. Ruby then executes the block, and passes the results of the block back as arguments. I have found this notation to be, by far, the most likely to cause you trouble.

When you use ({ :key => value }) you are explicitly passing a hash to the method. The parens explicitly define the method arguments, and the brackets are the standard notation for defining a hash (no ruby magic to auto-detect the hash-ness of the arguments).

I would say there is no definitive best/right way, but the ruby style guides seem to prefer the first version.