2012-09-13 13 views
2

我有一個哈希子類,其中我通常要初始化從另一哈希它的數據,並用於此:合併哈希散列子類 - 確保嵌套散列具有子類的屬性

class ValidatedJson < Hash 
    @schema = {} 
    def initialize(hash = {}) 
     super 
     JSON::Validator.validate!(@schema, hash, :validate_schema => true) 
     self.update(hash) 
    end 

    def [](key) 
     self.fetch(key) 
    end 
end 

然而,隨着只有self.update,任何嵌套散列的類型都是Hash,而不是具有我的子類ValidatedJson的屬性。任何人都知道快速有效的方法來確保這一點?

+1

遞歸地轉換嵌入的哈希值可能是你能做的最好的。 –

+0

是的,這就是我最終做的...... – grumpasaurus

回答