2011-04-21 79 views
0
{"table_no"=>nil, "name"=>"Kelly Shaye Gan", "account_id"=>1} 

我的意思是我确定有;只花了几个小时试图弄清楚。我认为答案在于:Most efficient way to map a specific string into a hash in Ruby,但我不是正则表达式的专家,所以我不确定插入扫描的表达式。有没有什么办法将这个字符串转换为红宝石?

+3

我认为必须要问的问题是:为什么你有摆在首位的是字符串? “eval”的解决方案通常表明设计存在缺陷。 – 2011-04-21 19:28:16

回答

8

如果您确信输入始终是安全的,可以EVAL它:

the_hash = eval(the_string)

+0

谢谢我实际上已经尝试过,我最终得到一个语法错误。我上面的例子会为你工作吗? – David 2011-04-25 12:27:18

+0

是的,它为我工作。 'the_string ='{“table_no”=> nil,“name”=>“Kelly Shaye Gan”,“account_id”=> 1}'' – 2011-04-25 14:30:12