Zuehlke / cookbook-windev

One cookbook with resources for setting up a Windows development environment
MIT License
7 stars 1 forks source link

Strings vs. Symbols as Keys #10

Open tknerr opened 9 years ago

tknerr commented 9 years ago

Looking at the README I see only Strings being used as the keys for the config hashes, e.g.:

{
  "software_depot": "software",
  "cache": [{ "source": "http://bin.repo/foo.zip", "save_as": "foo.zip" }]
}

I'm wondering whether using symbols would be more idiomatic (and also reads better when syntax-highlighted), e.g.:

{
  software_depot: "software",
  cache: [{ source: "http://bin.repo/foo.zip", save_as: "foo.zip" }]
}
tknerr commented 9 years ago

Not sure though whether the code handles them correctly if you switch to symbols, but chances are high I guess...

damphyr commented 9 years ago

I prefer simple strings to symbols and there are a couple of reasons.

Simple strings allows us to pass the json straight in and not have to use to_sym anywhere in the code.

They are also garbage-collectible (although Ruby 2.2 partially solves that problem).

As a side note:

  1. You can't use the new Hash syntax without symbols.
  2. Symbols are not such a good idea when the keys are not a fixed set (providing a json configuration file is by definition creating arbitrary keys). This relates directly to the fact that symbols are global and (apart from Ruby >2.2) and hold on to their memory forever.

Ergo, hash rockets forever!