Ruby-on-rails 如何抑制 Rails 控制台/irb 输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4678732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to suppress Rails console/irb outputs
提问by ghtn
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 :(
我正在 Rails 控制台的生产服务器中测试一些 db 条目,其中几乎所有命令都会产生大量的 o/p 行,因此 ssh 通道被挂起 :(
Is there a way to suppress the console/irb screenfuls?
有没有办法抑制控制台/irb screenfuls?
Thanks
谢谢
回答by intellidiot
You can append ; nilto 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 :)
实际上 irb 打印最后执行的语句的(返回)值。因此,在这种情况下,它只会打印 nil,因为 nil 是最后执行的有效语句 :)
回答by LarsDK
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"
回答by Mason Cloud
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 ctxgem first, though awesome_printis optional, of course.)
(注意:您必须先安装ctxgem,当然这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_printmode:
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 exitout or it, and you'll be back in the previous mode.
无论如何,您可以添加您喜欢的任何模式,当您完成该模式时,只需exit退出或它,您就会回到以前的模式。
Hope that was helpful! :)
希望这是有帮助的!:)
回答by Joshua Pinter
Supress Output, In General
一般情况下抑制输出
Also, depending on your needs, have a look at using quietlyor silence_streamfor suppressing output in general, not just in the irb/console:
此外,根据您的需要,查看使用quietly或silence_stream抑制一般输出,而不仅仅是在 irb/控制台中:
silence_stream(STDOUT) do
users = User.all
end
NOTE: quietlywill be deprecated in Ruby 2.2.0 and will eventually be removed.(Thanks BenMorganIO!)
注意:quietly将在 Ruby 2.2.0 中被弃用,最终将被删除。(感谢BenMorganIO!)
More information can be found here.
可以在此处找到更多信息。
回答by user3490179
irb --simple-prompt --noecho
--simple-prompt- Uses a simple prompt - just>>--noecho- Suppresses the result of operations
--simple-prompt- 使用一个简单的提示 - 只是>>--noecho- 抑制操作的结果
回答by schpet
running the following within irb works for me:
在 irb 中运行以下内容对我有用:
irb_context.echo = false

