Ruby on rails 如何抑制Rails控制台/irb输出
我在Rails控制台的生产服务器中测试了一些DB条目,几乎所有的命令都会产生大量的输出行,并导致ssh通道挂起Ruby on rails 如何抑制Rails控制台/irb输出,ruby-on-rails,ruby,irb,Ruby On Rails,Ruby,Irb,我在Rails控制台的生产服务器中测试了一些DB条目,几乎所有的命令都会产生大量的输出行,并导致ssh通道挂起 有没有办法抑制控制台/irb屏幕显示?您可以附加;对您的语句执行nil 例如: users = User.all; nil irb打印最后执行语句的返回值;因此,在这种情况下,它将只打印nil,因为nil是最后执行的有效语句。在寻找如何使irb/控制台输出静音的解决方案时,我还找到了以下答案: 沉默内部评级: conf.return_format = "" 默认输出: conf.r
有没有办法抑制控制台/irb屏幕显示?您可以附加
;对您的语句执行nil
例如:
users = User.all; nil
irb
打印最后执行语句的返回值;因此,在这种情况下,它将只打印nil
,因为nil
是最后执行的有效语句。在寻找如何使irb/控制台输出静音的解决方案时,我还找到了以下答案:
沉默内部评级:
conf.return_format = ""
默认输出:
conf.return_format = "=> %s\n"
限制为512个字符:
conf.return_format = "=> limited output\n %.512s\n"
在此处,将此添加到~/.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\u 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"}}}
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>
…是的,正是你所期待的
awesome\u打印模式:
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"}}}
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>
…哇,现在所有的东西都打印出来了
安静模式:
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"}}}
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>
。。。哇,一点输出都没有?很好
不管怎样,你可以添加任何你喜欢的模式,当你使用完该模式后,只要退出,你就会回到以前的模式
希望这会有帮助!:) 一般来说,suppress输出
此外,根据您的需要,可以考虑使用静默
或静默_流
来抑制一般输出,而不仅仅是在irb/控制台中:
silence_stream(STDOUT) do
users = User.all
end
注意:静音\u流
已在Rails 5+中删除。
注意:将在Ruby 2.2.0中被弃用,并最终被删除。
(谢谢!)
可以找到更多信息
解决Rails 5+的问题。
如上所述,静默\u流
不再可用,因为它不是线程安全的。没有线程安全的替代方案。但是,如果您仍然希望使用静默\u stream
,并且知道它不是线程安全的,并且没有以多线程方式使用它,那么可以手动将其添加回初始值设定项
config/initializer/silence\u stream.rb
#重新实现在Rails 5中删除的'silence_stream',因为它不是线程安全的。
#这也不是线程安全的,因此只能在单线程操作中使用它。
#看https://api.rubyonrails.org/v4.2.5/classes/Kernel.html#method-i-U溪。
#
def静音_流(流)
old_stream=stream.dup
stream.reopen(文件::NULL)
stream.sync=true
产量
确保
流。重新打开(旧流)
老_溪关闭
结束
-使用简单提示>>--简单提示
-抑制操作结果--noecho
irb_context.echo = false
真棒,一个更短的方法是分号后跟一个对象,比如
users=User.all;0
这只适用于返回的对象,而不适用于p和puts。这只是一个技巧,您可以使用count,就像Users.all.count
,只有一行输出,并且如果您想将输出存储在变量中,可以这样做Users=User.all;Users.all.count
非常有用。有没有可能在打开irb/rails控制台时设置此选项,即在中别名一个参数?您可以尝试将其放入$HOME/.irbrcNote中,在ruby 2.2.0中被弃用,并将被删除。@BenMorganIO在答案中添加了一条注释。谢谢你!