Tiny IRB improvement - max-size of return format

S

Stian Haklev

I'm not sure if it's appropriate to post here, but I found this very
useful, wanted to share it with people, and didn't know where else.

So I use IRB (and script/console) a lot for testing out regexps or small
code blocks, inspecting running Rails apps, and so on. One thing that
can be very annoying is when you run a command like

IRB> a = File.read("bigfile.txt")

and IRB spits out the entire contents of the file. The same happens when
you do

user = User.find_by_name("Stian")

in script/console, and the whole user object is inspected, often with
his/her files, messages, permissions etc added in

As far as I know, there is only a way of turning this return off, but
not of limiting it. I tried turning it off, but found myself doing

IRB> MD5::md5("Peter")
IRB>

over and over, which was annoying. So I wrote this little patch:

diff -r ../1.8-stian/irb/context.rb ./irb/context.rb
37,38d36
< @max_output_size = (IRB.conf[:MAX_OUTPUT_SIZE] ?
IRB.conf[:MAX_OUTPUT_SIZE] : 500)
<
119d116
< attr_accessor :max_output_size
diff -r ../1.8-stian/irb.rb ./irb.rb
297c297
< text = sprintf @context.return_format,
@context.last_value.inspect
---
printf @context.return_format, @context.last_value.inspect
299c299
< text = sprintf @context.return_format, @context.last_value
---
printf @context.return_format, @context.last_value
301d300
< puts text[[email protected]_output_size]


--

works on both Ruby 1.8.4 and 1.8.6. Basically it adds a new conf. option
- max_output_size, which defaults to 500, and restricts all results
based on this. Note that you can still see the whole thing by p object,
etc.

I just wanted to share it, because I found it very useful. And it's neat
to be able to hack on "Ruby" itself, even in a tiny way :)

thanks
Stian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top