Depth limited pp or to_yaml in ruby
Can 开发者_StackOverflow社区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.
精彩评论