Limit PP.pp recursion level depth

问题内容:

Related question:

Depth limited pp or to_yaml in ruby

I met a similar problem like the topic above.
In Ruby 2.3.4, the PP.pp method recursively print a string with hundreds of millions characters so my send mail function get timeout when try to print out an object, for example:

<%= PP.pp(@request.session.instance_variable_get(:@by)) %>

In Ruby 2.2.7 I used exactly the same send mail function and it would print one-level deep of object (similar to #.inspect but in pretty format).After days of investigating the different between PP.pp function in RB 2.3.4 and 2.2.7 I still cant figure out which changelog make this recursion depth become unlimited (?), and If we couldnt fix it, I wonder there’s any way to prettily print one level depth of object without using PP.pp. Thank you.

Reference:

2.3.4:

https://github.com/ruby/ruby/blob/v2_3_4/lib/pp.rb https://github.com/ruby/ruby/blob/v2_3_4/lib/prettyprint.rb

2.2.7:

https://github.com/ruby/ruby/blob/v2_2_7/lib/pp.rb https://github.com/ruby/ruby/blob/v2_2_7/lib/prettyprint.rb

问题评论:

    
What is @request? Could you give an example with actual example data?
    
@Stefan: ‘@request’ is just request object from controller (guides.rubyonrails.org/…), I want to pretily print exception in mail when an exception occurs. You could try printing out ” request.session.instance_variable_get(:@by).inspect” compare with using PP.pp with the same object. Thank you so much.
– ThuongLe
7 hours ago
    
I’d like to help you but I don’t want to set up a Rails project in order to do so. Please provide a Minimal, Complete, and Verifiable example.
    
@Stefan: Yeah I really appriciate that, but the example code is just PP.pp(request.session.instance_variable_get(:@by)) (request is global object). It includes nested child and grandchild objects so it became huge when recursively print the object (by PP.pp). With .inspect, it just print out the object’s level 1 child objects. Im new to ruby so my explain could be confusing. Thank you anyway.
– ThuongLe
6 hours ago

原文地址:

https://stackoverflow.com/questions/47748566/limit-pp-pp-recursion-level-depth

添加评论