【问题标题】:Is there any way to convert this string to hash in ruby?有什么方法可以将此字符串转换为 ruby​​ 中的哈希值?
【发布时间】:2013-05-23 20:35:13
【问题描述】:
{"table_no"=>nil, "name"=>"Kelly Shaye Gan", "account_id"=>1}

我的意思是我确定有;只是花了几个小时试图弄清楚。我认为答案就在这里:Most efficient way to map a specific string into a hash in Ruby,但我不是正则表达式方面的专家,所以我不确定要在扫描中插入哪个表达式。

【问题讨论】:

  • 我认为需要问的问题是:为什么你首先要有那个字符串? eval 的解决方案通常表明存在设计缺陷。

标签: ruby hash


【解决方案1】:

如果你确定输入总是安全的,你可以评估它:

the_hash = eval(the_string)

【讨论】:

  • 感谢我实际上已经尝试过 taht,但我最终得到了一个语法错误。我上面的例子对你有用吗?
  • 是的,它对我有用。 the_string = '{"table_no"=>nil, "name"=>"Kelly Shaye Gan", "account_id"=>1}'
猜你喜欢
  • 1970-01-01
  • 2013-01-22
  • 2010-11-05
  • 2015-04-02
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
相关资源
最近更新 更多