# Tiny IRB improvement - max-size of return format

**URL:** <https://www.ruby-forum.com/t/tiny-irb-improvement-max-size-of-return-format/104048>\
**Category:** Ruby\
**Created:** [May 13, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/tiny-irb-improvement-max-size-of-return-format/104048 "2007-05-13T16:58:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@stian](https://www.ruby-forum.com/u/stian)\
**Post date:** [May 13, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/tiny-irb-improvement-max-size-of-return-format/104048/1 "2007-05-13T16:58:18Z")

</div>

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[removed\_email\_address@domain.invalid\_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

---

<div class="post-metadata">

**Author:** ![stian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@stian](https://www.ruby-forum.com/u/stian)\
**Post date:** [May 13, 2007, 6:09pm UTC](https://www.ruby-forum.com/t/tiny-irb-improvement-max-size-of-return-format/104048/2 "2007-05-13T18:09:52Z")

</div>

Nice patch. Not to steal your thunder, but you can also mute irb’s  
response by tacking on “; nil” to the end of a particularly noisy  
return result:

> > a = IO.read(‘somebigfile’); nil  
> > =\> nil
> 
> > a.scan(/blah/)  
> > =\> …

---

<div class="post-metadata">

**Author:** ![stian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@stian](https://www.ruby-forum.com/u/stian)\
**Post date:** [May 13, 2007, 8:57pm UTC](https://www.ruby-forum.com/t/tiny-irb-improvement-max-size-of-return-format/104048/3 "2007-05-13T20:57:09Z")

</div>

Stian H. wrote:

> you do  
> IRB\>  
> diff -r …/1.8-stian/irb.rb ./irb.rb  
> \< puts text[removed\_email\_address@domain.invalid\_output\_size]  
> to be able to hack on “Ruby” itself, even in a tiny way 🙂
> 
> thanks  
> Stian

Nice. You don’t have to patch the installed irb to do this, if you don’t  
want to (maybe other users on your system will care, or maybe you don’t  
have permissions). You can, instead, monkey patch irb using your .irbrc  
file:

class IRB::Context  
attr\_accessor :max\_output\_size

alias initialize\_before\_max\_output\_size initialize  
def initialize(\*args)  
initialize\_before\_max\_output\_size(\*args)  
@max\_output\_size = (IRB.conf[:MAX\_OUTPUT\_SIZE] ?  
IRB.conf[:MAX\_OUTPUT\_SIZE] : 500)  
end  
end

class IRB::Irb  
def output\_value  
text =  
if @context.inspect?  
sprintf @context.return\_format, @context.last\_value.inspect  
else  
sprintf @context.return\_format, @context.last\_value  
end  
max = @context.max\_output\_size  
if text.size \< max  
puts text  
else  
puts text[removed\_email\_address@domain.invalid\_output\_size-1] + “…” + text[-2…-1]  
end  
end  
end

**END**

I also added a tweak so that you see the last character of the string.  
For example:

irb(main):001:0\> a = [0]\*10000  
=\> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…]
