Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Object#to_yaml_properties是一种方法,您可以使用该方法列出您希望将哪些实例变量序列化为 YAML,而无需重新实现整个序列化过程。如果你想排除某些实例变量,你可以使用super然后过滤掉你不想要的那些。
Object#to_yaml_properties
super
是否有等效方法允许您在调用时仅列出某些变量Object#inspect,或者我是否必须重新实现整个方法?
Object#inspect
vanilla Ruby 中没有这样的机制。
从好的方面来说,您可以实现它,然后在重新定义MyClass#inspect. 您不必为每个类完成所有工作,只需创建一个允许您设置检查的类级方法。
MyClass#inspect
或者您可以添加自己的Object#limited_inspect并重新定义Kernel#p(或添加您自己的快捷方式)以使用所有实例变量减去任何可能被排除的变量。
Object#limited_inspect
Kernel#p