简体   繁体   English

如何抑制 Rails 控制台/irb 输出

[英]How to suppress Rails console/irb outputs

I was testing some DB entries in our production server in Rails Console where almost all the commands were producing a huge number of lines of output and causing the ssh channel to hang.我正在 Rails 控制台中测试我们生产服务器中的一些数据库条目,其中几乎所有命令都产生大量 output 行并导致 ssh 通道挂起。

Is there a way to suppress the console/irb screenfuls?有没有办法抑制控制台/irb screenfuls?

You can append ; nil你可以追加; nil ; nil to your statements. ; nil到您的报表。

Example:示例:

users = User.all; nil

irb prints the return value of the last executed statement; irb打印最后执行的语句的返回值 thus in this case it'll print only nil since nil is the last executed valid statement.因此在这种情况下它只会打印nil因为nil是最后执行的有效语句。

In search of a solution how to silence the irb/console output, I also found an answer at austinruby.com :在寻找如何使 irb/console 输出静音的解决方案时,我还在austinruby.com 上找到了答案:

silence irb:沉默irb:

conf.return_format = ""

default output:默认输出:

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

limit to eg 512 chars:限制为例如 512 个字符:

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

Here, add this to your ~/.irbrc:在这里,将其添加到您的 ~/.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.) (注意:你必须先安装ctx gem,当然awesome_print是可选的。)

Now when you are on any console that uses irb, you can do the following:现在,当您在使用 irb 的任何控制台上时,您可以执行以下操作:

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: awesome_print模式:

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.无论如何,您可以添加您喜欢的任何模式,当您完成该模式时,只需exitexit ,您就会回到以前的模式。

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:此外,根据您的需要,看看使用quietlysilence_stream抑制输出一般,不只是在IRB /控制台:

silence_stream(STDOUT) do
  users = User.all
end

NOTE: silence_stream removed in Rails 5+.注意:在 Rails 5+ 中删除了silence_stream

NOTE: quietly will be deprecated in Ruby 2.2.0 and will eventually be removed.注意:在 Ruby 2.2.0 中quietly被弃用,最终将被删除。 (Thanks BenMorganIO !) (感谢BenMorganIO !)

More information can be found here .可以在此处找到更多信息。

Work Around for Rails 5+.为 Rails 5+ 工作。

As mentioned above, silence_stream is no longer available because it is not thread safe.如上所述, silence_stream不再可用,因为它不是线程安全的。 There is no thread safe alternative.没有线程安全的替代方案。 But if you still want to use silence_stream and are aware that it is not thread safe and are not using it in a multithreaded manner, you can manually add it back as an initializer.但是如果你仍然想使用silence_stream并且知道它不是线程安全的并且不是以多线程方式使用它,你可以手动将它添加回初始化程序。

config/initializer/silence_stream.rb

# Re-implementation of `silence_stream` that was removed in Rails 5 due to it not being threadsafe.
# This is not threadsafe either so only use it in single threaded operations.
# See https://api.rubyonrails.org/v4.2.5/classes/Kernel.html#method-i-silence_stream.
#
def silence_stream( stream )
  old_stream = stream.dup
  stream.reopen( File::NULL )
  stream.sync = true
  yield

ensure
  stream.reopen( old_stream )
  old_stream.close
end
irb --simple-prompt --noecho
  • --simple-prompt - Uses a simple prompt - just >> --simple-prompt - 使用简单的提示 - 只是>>
  • --noecho - Suppresses the result of operations --noecho - 抑制操作结果

在 irb 中运行以下内容对我有用:

irb_context.echo = false

Adding nil as a fake return value to silence output works fine, but I prefer to have some indication of what happened.nil作为假返回值添加到 silence output 可以正常工作,但我更喜欢对发生的事情有一些指示。 A simple count is often enough.一个简单的计数通常就足够了。 A lot of times, that's easily done by tacking on a count function. So when I'm doing something to a bunch of Discourse topics, I don't want a printout of each of the topic objects.很多时候,这很容易通过count function 来完成。所以当我对一堆 Discourse 主题做某事时,我不想打印输出每个主题对象。 So I add .count at the end of the loop:所以我在循环的末尾添加.count

Topic.where(...).each do |topic| 
...
end.count

Same thing if I'm just assigning something:如果我只是分配一些东西,也是一样的:

(users = User.all).count

Silencing output altogether (or making it something static like nil ) deprives me of useful feedback.完全沉默 output(或使它成为 static 之类的东西nil )剥夺了我有用的反馈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM