2011-04-21 76 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