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.
我可以在 Ruby 中限制 PP.pp 或 to_yaml 的递归深度吗?如果没有,我可以使用其他功能吗?
据我所知,没有简单的方法可以做到这一点。几乎每个 Ruby 对象都实现的#inspect 方法不受深度的限制。
您必须自己实现它,例如,您可以构建一个递归方法,该方法采用一个对象和一个整数值来表示当前深度。主要问题是,您需要以不同方式处理每个预期类的对象的输出以模拟其特定的#inspect 输出,因为您不能使用它们原来的#inspect 方法。