Looking for an alternative to eval
I'm new to ruby however it isn't really that drastic of a change coming from perl, anyways 've written a simple script to convert my go开发者_运维问答bs of perl Data::Dumper output into yaml configs, my problem is I'm using eval to accomplish this and seeing as I may like others to use this script one day I would like to eliminate eval for something more sane.
example:
input file contains
$VAR1 = { 'object' => { 'some_key' => 'some_value' } }
method to read it in
# read in file here ...
eval( stringified_file )
print $VAR1.to_yaml
output
object:
some_key: some_value
Thanks :)
On the Perl side you can output your data structures to YAML (I like YAML::Syck for this), and then read the data in as YAML on the Ruby side. That way you won't need to do an eval.
If for you're unable to change the source application to output YAML, use Kernel#load:
require 'yaml'
load 'dumped_file', true
puts $VAR1.to_yaml
精彩评论