Is there any difference between :key => "value" (hashrocket) and key: "value" (Ruby 1.9) notations?
If not, then I would like to use key: "value" notation. Is there a gem that helps me to convert from :x => to x: notations?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, there is a difference. These are legal:
but these are not:
You can also use anything as a key with
=>so you can do this:but you can’t do this:
The JavaScript style (
key: value) is only useful if all of your Hash keys are “simple” symbols (more or less something that matches/\A[a-z_]\w*\z/i, AFAIK the parser uses its label pattern for these keys).The
:$instyle symbols show up a fair bit when using MongoDB so you’ll end up mixing Hash styles if you use MongoDB. And, if you ever work with specific keys of Hashes (h[:k]) rather than just whole hashes (h = { ... }), you’ll still have to use the colon-first style for symbols; you’ll also have to use the leading-colon style for symbols that you use outside of Hashes. I prefer to be consistent so I don’t bother with the JavaScript style at all.Some of the problems with the JavaScript-style have been fixed in Ruby 2.2. You can now use quotes if you have symbols that aren’t valid labels, for example:
But you still need the hashrocket if your keys are not symbols.