我有以下代码。我传递的是Mongoid::Document
。
def clean_object(obj)
obj.instance_variables.each do |ivar|
obj.send(:remove_instance_variable, ivar) unless ivar == :@attributes
end
end
当我运行它时,我收到错误:
NameError Exception: instance variable @new_record not defined
但是当我跑obj.instance_variables
时,我可以清楚地看到它。
[:@new_record, :@attributes, :@changed_attributes, :@pending_nested, :@pending_relations, :@metadata
...
这里发生了什么以及如何删除这些实例变量?如何在obj.instance_variables中存在某些东西但在我尝试删除它时不存在?
另外,有时我可以使用:
obj.remove_instance_variable
但有时候它抱怨该方法是私有的,它迫使我使用obj.send(:remove_instance_variable
......
为了将其置于更大的上下文中,我需要将Mongoid::Document
序列化为memcached并将其拉回。我正在剥离序列化不需要的对象上不相关的膨胀(缓存文档关系)。