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

发布于 2024-10-11 11:41:26 字数 106 浏览 4 评论 0原文

我正在 Rails Console 的生产服务器中测试一些数据库条目,其中几乎所有命令都会生成大量输出行并导致 ssh 通道挂起。

有没有办法抑制控制台/irb screenfuls?

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

南巷近海 2024-10-18 11:41:27

在寻找如何使 irb/console 输出静音的解决方案时,我还在 austinruby.com

沉默 irb:

conf.return_format = ""

默认输出:

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

限制为例如 512 个字符:

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

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"
温暖的光 2024-10-18 11:41:27

在 irb 中运行以下命令对我有用:

irb_context.echo = false

running the following within irb works for me:

irb_context.echo = false
说好的呢 2024-10-18 11:41:27
irb --simple-prompt --noecho
  • --simple-prompt - 使用简单的提示 - 只是 >>
  • --noecho - 抑制操作结果
irb --simple-prompt --noecho
  • --simple-prompt - Uses a simple prompt - just >>
  • --noecho - Suppresses the result of operations
忆梦 2024-10-18 11:41:27

在这里,将其添加到您的 ~/.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

注意:您必须首先安装 ctx gem,当然,awesome_print 是可选的。)

现在,当您打开时任何使用 irb 的控制台,您都可以执行以下操作:

正常模式:

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"}}}

...是的,正是您所期望的。

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"
        }
    }
}

...哇,现在一切都打印得很棒! :)

安静模式:

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>

...哇,根本没有输出?好的。

不管怎样,你可以添加任何你喜欢的模式,当你完成该模式后,只需退出或它,你就会回到之前的模式。

希望这有帮助! :)

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! :)

将军与妓 2024-10-18 11:41:27

一般情况下抑制输出

此外,根据您的需要,请查看使用quietlysilence_stream来一般情况下抑制输出,而不仅仅是在irb/控制台中:

silence_stream(STDOUT) do
  users = User.all
end

注意:silence_stream 在 Rails 5+ 中被删除。

注意:quietly 将在 Ruby 2.2.0 中被弃用,并最终被删除。 (感谢BenMorganIO!)

可以找到更多信息此处

针对 Rails 5+ 进行解决。

如上所述,silence_stream 不再可用,因为它不是线程安全的。没有线程安全的替代方案。但是,如果您仍然想使用 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

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: silence_stream removed in Rails 5+.

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

More information can be found here.

Work Around for Rails 5+.

As mentioned above, silence_stream is no longer available because it is not thread safe. 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.

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
提笔书几行 2024-10-18 11:41:27

添加 nil 作为假返回值以静默输出效果很好,但我更喜欢有一些表明发生了什么。简单的计数通常就足够了。很多时候,通过添加一个 count 函数就可以轻松完成这一任务。因此,当我对一堆 Discourse 主题执行某些操作时,我不需要每个主题对象的打印输出。所以我在循环末尾添加 .count

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

如果我只是分配一些东西,也会发生同样的事情:

(users = User.all).count

完全沉默输出(或使其成为静态的东西,如 nil )剥夺了我的权利有用的反馈。

Adding nil as a fake return value to silence output works fine, but I prefer to have some indication of what happened. 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. So I add .count at the end of the loop:

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.

下雨或天晴 2024-10-18 11:41:27

尾随 ; 应该会抑制输出,正如许多人所期望的那样,这要归功于 Don从 2023 年起,当表达式以分号结尾时,不会回显表达式的结果 PR。

现在隐藏不相关的部分要容易得多。

$ irb --prompt simple
>> RUBY_VERSION
=> "3.3.1"
>> require 'date'
=> true
>> today = Date.today
=> #<Date: 2024-05-14 ((2460445j,0s,0n),+0s,2299161j)>
>> today.to_s
=> "2024-05-14"
>> RUBY_VERSION
=> "3.3.1"
>> require 'date';
>> today = Date.today;
>> today.to_s
 => "2024-05-14" 

(请注意,在旧版本上,irb 行为可能会有所不同 - 忽略尾随分号,或期望以下语句:)

>> RUBY_VERSION                                                                                
=> "3.1.2"
>> require 'date';
=> true
>> RUBY_VERSION
=> "2.3.3"
>> require 'date';
?>

Trailing ; should suppress the output, as expected by many, thanks to Don't echo an expression's result when it ends with a semicolon PR from 2023.

Hiding irrelevant parts is much easier now.

$ irb --prompt simple
>> RUBY_VERSION
=> "3.3.1"
>> require 'date'
=> true
>> today = Date.today
=> #<Date: 2024-05-14 ((2460445j,0s,0n),+0s,2299161j)>
>> today.to_s
=> "2024-05-14"
>> RUBY_VERSION
=> "3.3.1"
>> require 'date';
>> today = Date.today;
>> today.to_s
 => "2024-05-14" 

(Note that on older versions, irb behavior can vary - ignore trailing semicolon, or expect following statement:)

>> RUBY_VERSION                                                                                
=> "3.1.2"
>> require 'date';
=> true
>> RUBY_VERSION
=> "2.3.3"
>> require 'date';
?>
像你 2024-10-18 11:41:26

您可以附加 ; nil 到你的语句中。

示例:

users = User.all; nil

irb 打印最后执行的语句的返回值;因此在这种情况下,它只会打印 nil 因为 nil 是最后执行的有效语句。

You can append ; nil to your statements.

Example:

users = User.all; nil

irb prints the return value of the last executed statement; thus in this case it'll print only nil since nil is the last executed valid statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文