簡體   English   中英

通過虛線路徑從Ruby散列中深度刪除

[英]Deep delete from Ruby hash by dotted path

給定Ruby中嵌套哈希的哈希值(深度可能會有所不同):

hash = {"status_message"=>
         { "destination_does_not_exist"=>
           {"message_header"    => "Zielordner existiert nicht",
            "message_body"      => "Der Zielordner für das Backup existiert nicht mehr.",
            "corrective_action" => "Erstellen Sie den Zielordner."
           }
         }
       }

如何使用簡單的“點綴”表示法刪除鍵及其所有子值? 就像是:

path = "status_message.destination_does_not_exist.message_header"
hash.delete!(path)
path = path.split '.'
leaf = path.pop

path.inject(hash) {|h, el| h[el]}.delete leaf

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM