简体   繁体   中英

Depth limited pp or to_yaml in ruby

Can I limit the recursion depth of PP.pp or to_yaml in Ruby? 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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