简体   繁体   中英

Array to key value pair (Hash)

>> description

=> [" name=\"margin-top\" content=\"1\" />\r\n", " name=\"oreintation\" content=\"horizontal\" />\r\n", " name=\"paper-height\" content=\"6\" />\r\n", " name=\"paper-width\" content=\"5\" />\r\n", " name=\"margin-left\" content=\"3\" />\r\n"]

description is of type array

I need key as margin-top value 1 .
I need key as oreintation value oreintation and so on, how to get this done?

Use

result = {}
description.each{|val|
 result[val.split("\"")[1]] = val.split("\"")[3]
}
result # contains expected hash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM