这里是string
需要转换成hash
.
"{:status => {:label => 'Status', :collection => return_misc_definitions('project_status') } }"
我们不能使用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') } }")
这两条线之间没有功能差异,它们产生完全相同的结果。
h1 == h2 # => true
当然,如果可以的话,不要使用 ruby 哈希值的字符串表示。使用 JSON 或 YAML。它们更安全(不需要eval
).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)