This question already has an answer here:
Here is the string
which needs to convert into a hash
.
"{:status => {:label => 'Status', :collection => return_misc_definitions('project_status') } }"
We can not use eval
because eval
will execute the method return_misc_definitions('project_status')
in the string. Is there pure string operation to accomplish this conversion in ruby/rails? Thanks for help.
As mentioned earlier, you should use
eval
. Your point abouteval
executingreturn_misc_definitions
doesn't make sense. It will be executed either way.There's no functional difference between these two lines, they produce exactly the same result.
Of course, if you can, don't use string represenstation of ruby hashes. Use JSON or YAML. They're much safer (don't require
eval
).