How to limit the output of a Ruby `pp` call?

Go To StackoverFlow.com

3

I've got a recursive object (Cucumber::Rails::World.scenario) which I'd like to inspect (in order to find the tags belonging to the current scenario). scenario.inspect never finishes, and pp scenario prints so fast that even with a quick series of Ctrl-c it fills about three thousand lines. How can I limit the output?

2012-04-05 14:49
by l0b0


3

Use pretty_inspect to get it as a string, then get only the first n characters:

pp_output = scenario.pretty_inspect; nil
puts pp_output[0..n]; nil

Note the trailing nils cause IRb to display the return value nil rather than the entire object, which cleans up output substantially.

For even more flexibility, save it to a file:

File.open "pp-output.txt", "w" do |f|
  f.puts scenario.pretty_inspect
end

Then view in the pager of your choice:

$ less pp-output.txt
2012-04-05 15:10
by Andrew Marshall


0

Insert gets at certain points (in between small chunks of pp). Then, you can go through them slowly by inputting enter after each chunk.

2012-04-05 15:07
by sawa
Ads