Stian Haklev wrote:
> 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[0.. / context.max_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
> 

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[0.. / context.max_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...]

-- 
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407