我正在使用 Mongo 并将哈希存储在数据库中。但是后来当我检索哈希时,我不能再使用我的对象方法了?如何将检索到的 Hash 从数据库中转换为 TraitScore ???
class TraitScore < Hash
def initialize(attrs = {}, options = nil)
self['net'] = attrs[:net] || 0.0
self['total'] = attrs[:total] || 0.0
self['score'] = attrs[:score] || 0.0
end
def inc_net(val)
self['net'] += val
end
def inc_total(val)
self['total'] += (val || 0).abs
end
def set_score(score)
self['score'] = score
end
end