Question

I'm stuck with a pretty weird problem.

I was testing some db entries in our production server in Rails Console where almost all the commands were resulting a huge number of o/p lines, due to which the ssh channel was getting hanged :(

Is there a way to suppress the console/irb screenfuls?

Thanks

Was it helpful?

Solution

You can append ; nil to all your your commands/statements.

Example:

users = User.all; nil

Actually irb prints the (return) value of the last executed statement. Thus in this case it'll print only nil as nil is the last executed valid statement :)

OTHER TIPS

In search of a solution how to silence the irb/console output, I also found an answer at austinruby.com:

silence irb:

conf.return_format = ""

default output:

conf.return_format = "=> %s\n"

limit to eg 512 chars:

conf.return_format = "=> limited output\n %.512s\n"

Here, add this to your ~/.irbrc:

require 'ctx'
require 'awesome_print'

module IRB
  class Irb    
    ctx :ap do
      def output_value()
        ap(@context.last_value)
      end
    end
    ctx :puts do
      def output_value()
        puts(@context.last_value)
      end
    end
    ctx :p do
      def output_value()
        p(@context.last_value)
      end
    end
    ctx :quiet do
      def output_value()
      end
    end
  end
end

def irb_mode(mode)
  ctx(mode) { irb }
end

(Note: You must install the ctx gem first, though awesome_print is optional, of course.)

Now when you are on any console that uses irb, you can do the following:

Normal mode:

irb(main):001:0> { this:'is a complex object', that:[ { will:'probably'}, { be:'good to read' } ], in:{ some:{ formatted:'way'} } }

=> {:this=>"is a complex object", :that=>[{:will=>"probably"}, {:be=>"good to read"}], :in=>{:some=>{:formatted=>"way"}}}

...yep, just what you expect.

awesome_print mode:

irb(main):002:0> irb_mode(:ap)
irb#1(main):001:0> { this:'is a complex object', that:[ { will:'probably'}, { be:'good to read' } ], in:{ some:{ formatted:'way'} } }

=> {
    :this => "is a complex object",
    :that => [
        [0] {
            :will => "probably"
        },
        [1] {
            :be => "good to read"
        }
    ],
      :in => {
        :some => {
            :formatted => "way"
        }
    }
}

...wow, now everything is printing out awesomely! :)

Quiet mode:

irb#1(main):002:0> irb_mode(:quiet)
irb#1(main):001:0> { this:'is a complex object', that:[ { will:'probably'}, { be:'good to read' } ], in:{ some:{ formatted:'way'} } }
irb#1(main):002:0>

... whoah, no output at all? Nice.

Anyways, you can add whatever mode you like, and when you're finished with that mode, just exit out or it, and you'll be back in the previous mode.

Hope that was helpful! :)

Supress Output, In General

Also, depending on your needs, have a look at using quietly or silence_stream for suppressing output in general, not just in the irb/console:

silence_stream(STDOUT) do
  users = User.all
end

NOTE: quietly will be deprecated in Ruby 2.2.0 and will eventually be removed. (Thanks BenMorganIO!)

More information can be found here.

$ irb --simple-prompt --noecho

running the following within irb works for me:

irb_context.echo = false
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top