Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strings vs. Symbols as Keys #10

Open
tknerr opened this issue Jun 5, 2015 · 2 comments
Open

Strings vs. Symbols as Keys #10

tknerr opened this issue Jun 5, 2015 · 2 comments

Comments

@tknerr
Copy link
Member

tknerr commented Jun 5, 2015

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
Copy link
Member Author

tknerr commented Jun 5, 2015

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

@damphyr
Copy link
Member

damphyr commented Jun 8, 2015

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants