ght*_*htn 85 ruby ruby-on-rails irb
我遇到了一个非常奇怪的问题.
我在Rails控制台的生产服务器中测试了一些数据库条目,其中几乎所有的命令都产生了大量的o/p行,因此ssh通道被挂起了:(
有没有办法抑制控制台/ irb屏幕?
谢谢
int*_*iot 181
你可以追加; 没有你的所有命令/声明.
例:
users = User.all; nil
Run Code Online (Sandbox Code Playgroud)
实际上,irb打印最后执行的语句的(返回)值.因此,在这种情况下,它将只打印nil,因为nil是最后执行的有效语句:)
小智 31
为了寻找如何使irb/console输出静音的解决方案,我也在austinruby.com找到了答案:
沉默irb:
conf.return_format = ""
Run Code Online (Sandbox Code Playgroud)
默认输出:
conf.return_format = "=> %s\n"
Run Code Online (Sandbox Code Playgroud)
限制为例如512个字符:
conf.return_format = "=> limited output\n %.512s\n"
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
(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"}}}
Run Code Online (Sandbox Code Playgroud)
...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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
...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>
Run Code Online (Sandbox Code Playgroud)
... whoah, no output at all? Nice.
无论如何,您可以添加您喜欢的任何模式,当您完成该模式时,只需exit输出或它,您将回到之前的模式.
希望有用!:)
小智 9
irb --simple-prompt --noecho
Run Code Online (Sandbox Code Playgroud)
--simple-prompt- 使用简单的提示 - 只需>>--noecho- 抑制运算结果另外,根据您的需要,一般来看看使用quietly或silence_stream抑制输出,而不仅仅是在irb/console中:
silence_stream(STDOUT) do
users = User.all
end
Run Code Online (Sandbox Code Playgroud)
注意:quietly将在Ruby 2.2.0中弃用,最终将被删除. (谢谢BenMorganIO!)
更多信息可以在这里找到.