Ruby 1.9 new Hash syntax is not always useful

Hashes allow an alternate syntax form when your keys are always symbols.
Instead of

options = { :font_size => 10, :font_family => “Arial” }
You could write it as:

options = { font_size: 10, font_family: “Arial” }

Really ? :slight_smile:

Let me see :-

[email protected]:~/Ruby> pry
[1] pry(main)> { :$foo => 12 }
=> {:$foo=>12}
[2] pry(main)> { $foo: 12 }
SyntaxError: unexpected ‘:’, expecting =>
{ $foo: 12 }
[2] pry(main)>

Doco is not complete. It says use the old syntax, which is always safe.

Arup R.

Debugging is twice as hard as writing the code in the first place.
if you write the code as cleverly as possible, you are, by definition,
smart enough to debug it.

–Brian Kernighan

I run into this all the time writing Mongo queries for MongoMapper. I
up just using the hash rocket syntax always

On Wed, Jul 9, 2014 at 1:23 PM, Arup R.
[email protected]

Hi Arup,

Perhaps it’s not worth the work to deambiguate this with the global
variable syntax!?
You should agree that is not “that” common to have a Symbol with a
name beginning with a dollar sign.
But, if you have a good use case, you should search the Ruby Issue
Tracking System (
If nothing on it, fire a issue with a detailed use case.

Abinoam Jr.

PS: Looking quickly I found this It seems related.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs