phillee
phillee

Reputation: 2227

ruby new hash colon notation with string keys

Using ruby 1.9.3, string keys don't seem to work with Hash colon notation:

1.9.3p194 :005 > {abc: 5}
 => {:abc=>5} 

1.9.3p194 :004 > {'abc': 5}
SyntaxError: (irb):4: syntax error, unexpected ':', expecting tASSOC
{'abc': 5}
       ^

I think I'm running the right version of Ruby

1.9.3p194 :006 > RUBY_ENGINE
 => "ruby" 
1.9.3p194 :007 > RUBY_VERSION
 => "1.9.3" 

Upvotes: 24

Views: 16615

Answers (1)

Chowlett
Chowlett

Reputation: 46667

That's correct - the new colon notation for hashes only works when the keys are symbols.

Sorry, that's just how it is.

Update: general symbols are supported using the new notation in ruby 2.2 and later (strings as keys still aren't):

irb
2.2.2 :001 > {'abc': 5}
=> {:abc=>5} 

Upvotes: 42

Related Questions