help about yaml/eval code

D

dave

ddir = ENV['DOCUMENT_ROOT'] + '/data'

Find.find(ddir){|f|
if (f =~ /.yml$/) # has yml extention
eval "#{File.basename(f.delete('.yml'))} = YAML.load(File.new(f))"
else
next
end
}

I serched on ruby-talk but i did not find any solution.

How this code can be written (or re-written)?

tnx.


--
here are more things in heaven and earth,
horatio, than are dreamt of in your philosophy.
 
E

ES

Le 15/5/2005 said:
ddir =3D ENV['DOCUMENT_ROOT'] + '/data'

Find.find(ddir){|f|
if (f =3D~ /.yml$/) # has yml extention
eval "#{File.basename(f.delete('.yml'))} =3D YAML.load(File.new(f))"
else
next
end
}

I serched on ruby-talk but i did not find any solution.

How this code can be written (or re-written)?

I am not sure if I should be answering since I seem to be
even denser than usual but to me it looks like you are
trying to assign the contents of a file to a variable
named after the file, is that right? This might work:

files =3D {}
Find.find("#{ENV[DOCUMENT_ROOT]}/data") do |path|
if path =3D~ /.yml$/
File.open path do |file|
# #eval or #instance_variable_set should work here, too
files[file.basename[0...-4]] =3D YAML.load file
end
end
end


E
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top