I want to truncate #inspect output in irb (a large output must be cropped to MAX_LEN).
Currently, I override :inspect, :to_s methods for all specific objects.
Is there are other solution?
- change $stdout ?
- other?
I want to truncate #inspect output in irb (a large output must be cropped to MAX_LEN).
Currently, I override :inspect, :to_s methods for all specific objects.
Is there are other solution?
For a clean solution,
gem install hirb
. hirb pages irb's returned values if they get too long.If you want to monkeypatch irb:
I don't recommend this because it's a hack and breaks any time gems like ap and hirb are required.
Instead of monkeypatching irb, I'd recommend trying ripl, an irb alternative that is meant to extended. The above as a ripl plugin would be:
With this plugin, you could require it as needed or add to your ~/.riplrc if you want to always use it.
If you're just in IRB - you could define a monkeypatch in irb itself and or load a file that monkeypatches inspect via 'load'. This way you keep it out of your core codebase but you still get the functionality you need w/o having to override inspect in every class you wish to inspect....
For rails 3.1.1+, place the code below in helpers/irb_helper.rb
If you'd like to customize your output more, check irb's source at https://github.com/Ruby/Ruby/blob/trunk/lib/irb.rb
If it's because you have a nested hash or something that's hard to decipher, try awesome_print. You can make it the default output formatter in irb by placing the following in your .irbrc:
This makes objects with lots of data easy to decipher in IRB.
Even if you don't use awesome_print, you can truncate output using this same technique so you don't have to override to_s in your code.
Your solution is good.
It involves no dark magic, which might make the code less understandable and error-prone.
I sometimes modify the objects themselves (via a module called
BoringInspect
which Iinclude
into the relevant classes) so that exception messages are also manageable.