简体   繁体   English

红宝石中的深度限制pp或to_yaml

[英]Depth limited pp or to_yaml in ruby

Can I limit the recursion depth of PP.pp or to_yaml in Ruby? 我可以在Ruby中限制PP.pp或to_yaml的递归深度吗? If not is there another function I can use? 如果没有,我可以使用另一个功能吗?

As far as I know there is no easy way to do this. 据我所知,没有简单的方法可以做到这一点。 The #inspect method which almost every Ruby object implements cannot be limited by depth. 几乎每个Ruby对象实现的#inspect方法都不受深度限制。

You would have to implement that by yourself, for example you could build a recursive method that takes the an object and an Integer value to represent the current depth. 您必须自己实现它,例如,您可以构建一个递归方法,该方法接受一个对象和一个Integer值来表示当前深度。 The main problem is, that you need to handle output for objects of each expected class differently to simulate their specific #inspect output, as you can't use their original #inspect method. 主要问题是,您需要以不同方式处理每个预期类的对象的输出以模拟其特定的#inspect输出,因为您无法使用其原始的#inspect方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM