5

Given such hash with nested hashes in Ruby (deep may vary):

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."
           }
         }
       }

How can I delete key and all its children values using simple "dotted" notation? Something like:

path = "status_message.destination_does_not_exist.message_header"
hash.delete!(path)
Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109

1 Answers1

7
path = path.split '.'
leaf = path.pop

path.inject(hash) {|h, el| h[el]}.delete leaf
Yossi
  • 11,778
  • 2
  • 53
  • 66
  • Good catch, thanks. I just learning ruby, so didn't recognise this as a simple tree. –  Jun 19 '13 at 21:08
  • Yossi, may be there is some information around operating with trees using Ruby? How to represent them using hashes and so on/ –  Jun 20 '13 at 06:20
  • Never seen any resources on that, sorry. – Yossi Jun 20 '13 at 09:31