Lower verbosity in IRB?

OK, this is probably a very basic question, but how do ou go about
lowering the verbosity of irb?

For instance, if you wanted to read in a decent sized log file to scan
it for anomalies, you may do something like this:

logfile=File.open(“backup_08192008.log”){|f| f.read()}

Normally, its fine that irb will barf out the contents of the file to
screen. Normally it’s even useful…
In this case however, it’s really really bad. The log file I’m
looking at is about 84 megs.

Thanks,
Kyle

logfile=File.open(“backup_08192008.log”){|f| f.read()}

Normally, its fine that irb will barf out the contents of the file to
screen. Normally it’s even useful…

You need to get the statement to evaluate to something short, preferably
nil.

(log = File.read(‘backup_08192008.log’)) && nil

Then irb will print nil but log will still contain the data.

James
http://blog.jcoglan.com

A simple workaround is to surround your code with begin/end statements.

eg…

begin
logfile=File.open(“backup_08192008.log”){|f| f.read()}
logfile.size
end

will only print the logfile’s size, and won’t print the logfile
contents.

hope that’s useful
-patrick

On Thursday 21 August 2008, Kyle S. wrote:

In this case however, it’s really really bad. The log file I’m
looking at is about 84 megs.

Thanks,
Kyle

You need to create a .irbrc file in your home directory with the line

IRB.conf[:ECHO]=false

This file is read by irb at startup. If you’re on windows, then I don’t
know
where this file should be put. I also suspect that there should be a way
to do
it from within irb, but I don’t know how.

I hope this helps

Stefano

You can achieve the same effect at runtime without messing with .irbrc
if
this is not something you want to do permanently:

irb(main):001:0> conf.echo = false
irb(main):002:0> log = File.open(“filetoread”){|f| f.read}
irb(main):003:0>

Zhao

On Thu, Aug 21, 2008 at 10:07 AM, Stefano C.

On Aug 21, 2008, at 1:07 PM, James C. wrote:

Then irb will print nil but log will still contain the data.

James
http://blog.jcoglan.com
jcoglan (James Coglan) · GitHub

I tend to just add “;nil” to the end as in:

log = File.read(‘backup_08192008.log’); nil

You could also add: ;log.size
or: ;log.length

or some such. The only downside is that _ won’t be set to the thing
you probably want, but since you’re assigning to log anyway, that
shouldn’t matter. (I often follow a command with: x=_;nil when I
forget to capture to a variable.)

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Le 21 août 2008 à 19:07, James C. a écrit :

[Note: parts of this message were removed to make it a legal post.]

logfile=File.open(“backup_08192008.log”){|f| f.read()}

Normally, its fine that irb will barf out the contents of the file to
screen. Normally it’s even useful…

You need to get the statement to evaluate to something short, preferably
nil.

irb prints the last statement, in fact. Just adding ; nil at the end
works.

logfile=File.open(“backup_08192008.log”){|f| f.read()} ; nil

(BTW, for the original poster, to slurp a whole file, var =
File.read("…") works too.)

Fred

On Thu, Aug 21, 2008 at 12:36 PM, F. Senault [email protected] wrote:

(BTW, for the original poster, to slurp a whole file, var =
File.read(“…”) works too.)

Fred, nice trick with .read I like that.

Anyway, as far as what you, and several other posters posted, I
basically didn’t want to mess about with that sort of thing anymore, I
wanted the “right way” ™. Reason being, you always seem to forget
to put in the lines for little workarounds like that when you start,
and end up having to wait a few minutes for the 84 megs of text to get
shoved through ssh and rendered on the terminal. Which is what
happened to me when I posted :slight_smile:

What Stephan & Zhao mentioned is exactly what I was looking for.

Thanks all!

PS: typing when Joel posted… That is a really, really neat trick.
I’m gonna try that on my .irbc soon! Heck, maybe that should be in
the default irbc. Hint hint if any maintainers read random posts
like this…

Kyle S. wrote:

OK, this is probably a very basic question, but how do ou go about
lowering the verbosity of irb?

You can set up irb to limit the number of chars it prints. Put the
following code in your .irbrc. This was originally written by Stian
Haklev.

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[[email protected]_output_size-1] + “…” + text[-2…-1]
end
end
end

Joel VanderWerf wrote:

Kyle S. wrote:

OK, this is probably a very basic question, but how do ou go about
lowering the verbosity of irb?

You can set up irb to limit the number of chars it prints. Put the
following code in your .irbrc. This was originally written by Stian H…

I hadn’t looked at that code in a long time. The following is cleaned up
a bit, and with a sample output at the end (you can see how it guesses
that the last char is a delimiter of some kind).

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] || 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…max-1] + “…” + text[-2…-1]
end
end
end

END

irb(main):001:0> (1…1000).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
121,…]