How to limit the output of a ruby `pp` call?
I have a recursive object (Cucumber :: Rails :: World.scenario) that I would like to inspect (to find the tags that belong to the current script). scenario.inspect
never ends, but pp scenario
prints so quickly that even with a fast burst Ctrl- cit fills about three thousand lines. How can I limit withdrawal?
source to share
Use pretty_inspect
to get it as a string, then only get the first n characters:
pp_output = scenario.pretty_inspect; nil
puts pp_output[0..n]; nil
Note that trailing nil
causes IRb to render the return value nil
rather than the entire object, which essentially cleans up the output.
For more flexibility, save it to a file:
File.open "pp-output.txt", "w" do |f|
f.puts scenario.pretty_inspect
end
Then view in a pager of your choice:
$ less pp-output.txt
source to share