5

TL;DR — The Question

Is { 'symbol name': 5 } and { "symbol name": 5 } valid and well-defined Ruby 2 syntax for Hashes?

6 Notations for Hashes, 2 of them yet unknown

In Ruby 2, the following Hash literal notations are equivalent:

{ :my_key => 5 }
{ my_key: 5 }
{ :'my_key' => 5 }
{ :"my_key" => 5 }
  1. The first line is the generic Hash literal notation (that also works for non-symbol keys), with the default Symbol literal notation for the key.
  2. The second line is the new short-hand notation introduced by Ruby 2 Ruby 1.9 for Hashes with Symbols as keys.
  3. The third line is again the generic Hash literal notation, with an alternate Symbol literal notation for the key. (This alternate Symbol literal notation is handy if you need to have spaces or other fancy characters in your symbol names.)
  4. The fourth line is a slight variation of above, which also allows for string interpolation in the symbol name.

The first two notations are documented on the Core API page for Hash. The fourth notation is just plugging in an alternate Symbol literal notation (which is documented in the Core API page for Symbol) to the first Hash notation, so it isn't really a different notation for Hash literals. Same goes for the third notation. That the single-quoted-string Symbol literal notation isn't mentioned on the Symbol Core API page doesn't bother me too much, as it seems to work just like I'd expect.

But recently I noticed the following notations work too, and are also equivalent to the ones above:

{ "my_key": 5 }
{ 'my_key': 5 }

While it is kinda consistent (and works like I'd have expected, had I expected this to be valid at all) and probably useful, I find this remarkable enough to be a bit surprised. I couldn't find any documentation on this syntax, and this syntax isn't just constructed by plugging documented notations into other documented notations like the third and fourth notations above. (It's more like 'merging' the second with the third or fourth notation.) Thus I wondered:

Is this this just my Ruby interpreter (MRI ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux]) being nice about an undefined syntax, or is this a behavior I can expect from any complying Ruby 2 implementation?

(Not sure whether this question makes sense, if it is as Brian Shirai claims that "Ruby Is What [MRI] Does".)

Community
  • 1
  • 1
das-g
  • 9,718
  • 4
  • 38
  • 80
  • That syntax became available in Ruby 2 – jollarvia Jul 17 '15 at 19:32
  • 2
    @jollarvia: You mean 2.2, 2.0 and 2.1 see `{'s': v }` as a syntax error. – mu is too short Jul 17 '15 at 19:48
  • 1
    You might consider adding examples of this new syntax to the docs by yourself. http://documenting-ruby.org/ is a helpful resource. – cremno Jul 17 '15 at 20:01
  • Because it isn't relevant to my question with what version the JavaScript-like syntax (dubbed "second notation" in the question) was introduced, I've [corrected the version number](http://stackoverflow.com/revisions/31483296/2) according to [cremno's answer](http://stackoverflow.com/a/31483601/674064). – das-g Jul 17 '15 at 20:15

2 Answers2

7
{ :my_key => "my value" } 
{ my_key: "my value" }
{ :'my_key' => "my value" }
{ :"my_key" => "my value" }

None of the above lines uses 2.x-only syntax. They are all also valid 1.9 syntax. (See demonstration.)

{ "my_key": "my value" }
{ 'my_key': "my value" }

That's feature request #4276 which landed in 2.2. That means it's invalid syntax in 2.1 or even older versions. It also means that an implementation that claims to implement 2.2 has to support it.

cremno
  • 4,672
  • 1
  • 16
  • 27
0

If you are all about double vs single quotation marks, why not also do

{'my_key': 'my value' } and add yet another method of constructing a hash to your list?

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
Eupatoria
  • 130
  • 1
  • 9
  • My question could indeed have done with just mentioning *either* the third (`{:'key' =>5}`) **or** the fourth (`{:"key"=>5}) syntax. I only mentioned both for completeness. Neither I nor my question are "all about double vs. single" quotes. Rather all about Hash literal syntax, especially concerning their key literals. Thus why I didn't vary the value literal. – das-g Jul 17 '15 at 19:49
  • I've [changed the examples in the question](http://stackoverflow.com/revisions/31483296/3) to use integers instead of strings for the Hash values, so that there is only one literal notation for the values. – das-g Jul 17 '15 at 20:22
  • Got it. It is valid syntax. But in {'symbol name': '5'} the class of '5' will be String; in {'symbol name': 5}, the class of 5 will be Fixnum. – Eupatoria Jul 17 '15 at 20:36