2013-06-19 5 views
5

रूबी में नेस्टेड हैश के साथ इस तरह हैश को देखते हुए द्वारा रूबी हैश से हटाना (गहरी भिन्न हो सकते हैं):दीप बिंदीदार पथ

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) 
+0

http://stackoverflow.com/questions/6423484/how-do-i-convert-hash-keys-to-method-names –

उत्तर

5
path = path.split '.' 
leaf = path.pop 

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

अच्छा पकड़ है, धन्यवाद के संभावित डुप्लिकेट । मैं सिर्फ रूबी सीख रहा हूं, इसलिए इसे एक साधारण पेड़ के रूप में नहीं पहचाना। –

+0

बीटीडब्ल्यू, जिसने मेरी समस्या हल की, धन्यवाद! –

+0

योसी, रूबी का उपयोग करके पेड़ के साथ काम करने के आसपास कुछ जानकारी हो सकती है? हैश का उपयोग करके उनका प्रतिनिधित्व कैसे करें और इतने पर/ –

संबंधित मुद्दे