关键在ruby新哈希

Nav*_*eed 2 ruby hash ruby-on-rails ruby-1.9.2

Ruby 1.9.2的新哈希语法存在一个奇怪的问题.如何在1.9.2中将任何Object作为密钥放入哈希?

在1.8.7哈希它工作:

a="b" 
{"a" => "some",a => "another value",:a => "3rd value"}
Run Code Online (Sandbox Code Playgroud)

但是在1.9.2中>我们不能(或者如果我错了怎么办?)

1.9.2哈希:

{a: "some"} =>  {:a=>"s"} #it convert to old hash format
Run Code Online (Sandbox Code Playgroud)

a="a" 
{a: "..."} # This doesn't work

{"a": "some value"} => syntax error, unexpected '}', expecting $end
from /home/naveed/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'

{1: "s"} =>

SyntaxError: (irb):11: syntax error, unexpected ':', expecting tASSOC {1: "s"}
Run Code Online (Sandbox Code Playgroud)

jdo*_*doe 6

在Ruby 1.9中,只允许: 用作键的符号后放置冒号!

任何对象都可以使用箭头=>,甚至是符号.