use*_*363 7 ruby string hash ruby-on-rails
这是string需要转换成一个hash.
"{:status => {:label => 'Status', :collection => return_misc_definitions('project_status') } }"
Run Code Online (Sandbox Code Playgroud)
我们无法使用eval因为eval会return_misc_definitions('project_status')在字符串中执行该方法.是否有纯字符串操作来完成ruby/rails中的这种转换?感谢帮助.
如前所述,你应该使用eval.你关于eval执行的观点return_misc_definitions没有意义.它将以任何一种方式执行.
h1 = {:status => {:label => 'Status', :collection => return_misc_definitions('project_status') } }
# or
h2 = eval("{:status => {:label => 'Status', :collection => return_misc_definitions('project_status') } }")
Run Code Online (Sandbox Code Playgroud)
这两条线之间没有功能差异,它们产生完全相同的结果.
h1 == h2 # => true
Run Code Online (Sandbox Code Playgroud)
当然,如果可以,请不要使用红宝石哈希的字符串表示.使用JSON或YAML.它们更安全(不要求eval).
| 归档时间: |
|
| 查看次数: |
14579 次 |
| 最近记录: |