Depth limited by pp or to_yaml in ruby

Is it possible to limit the depth of recursion of PP.pp or to_yaml in Ruby? If there is no other function that I can use?

+4
source share
1 answer

As far as I know, there is no easy way to do this. The #inspect method, which is implemented by almost every Ruby object, cannot be limited by depth.

You would need to implement this yourself, for example, you could build a recursive method that takes an object and an Integer value to represent the current depth. The main problem is that you need to handle the output for the objects of each expected class differently to mimic your specific #inspect output, since you cannot use your original #inspect method.

0
source

Source: https://habr.com/ru/post/1334358/


All Articles