[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

help about yaml/eval code

dave

5/15/2005 8:01:00 PM



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.


2 Answers

ES

5/15/2005 8:43:00 PM

0


Le 15/5/2005, "dave" <dave.m@email.it> a écrit:
>
>
>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)?

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


>tnx.

E

--
template<typename duck>
void quack(duck& d) { d.quack(); }


dave

5/16/2005 1:21:00 PM

0



> files[file.basename[0...-4]] = YAML.load file
it sounds good, tnx again.

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