ruby - How to add key-value pairs to hash in a loop -


i want add key-value pairs hash in loop. hash looks this

[ {'filename' => filename1, 'filelocation'=> filelocation1}, {'filename' => filename2, 'filelocation'=> filelocation2} ] 

i using following code it. not add values, last key-value pair.

filedetails= {} doc = rexml::document.new args[0] doc.elements.each("node/congfigurations/config") {  |config|      filename= config.elements["@filename"].value     filelocation= config.elements["@location"].value     filedetails={'filename' => filename, 'filelocation'=> filelocation} } 

how can collect values?

first, expected output not hash, array. declare this

filedetails = [] 

and inside loop, make change

filedetails << {'filename' => filename, 'filelocation'=> filelocation} # push hash array 

edit

"it not add values, last key-value pair"

this because, using assignment operator. every time in loop, variable gets reassigned, , in end have last assigned value.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -