如何减少堆栈跟踪中的输出?

时间:2013-07-10 19:11:17

标签: ruby

当出现错误时,有没有办法减少Ruby提供的输出量?

例如:

rspec bowling_spec.rb 
/Users/snowcrash/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- bowling (LoadError)
  from /Users/snowcrash/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
  from /Users/snowcrash/Developer/Code/Ruby/RSpec/bowling_spec.rb:2:in `<top (required)>'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `load'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `each'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `load_spec_files'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/command_line.rb:22:in `run'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/runner.rb:80:in `run'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/runner.rb:17:in `block in autorun'

我感兴趣的是第一行,cannot load such file -- bowling (LoadError)。理想情况下,我希望Ruby不要吐出from行的其余部分。

这可能吗?

2 个答案:

答案 0 :(得分:2)

做这样的事情:

module Kernel
  at_exit do
    case $!
    when nil, SystemExit, Interrupt
    else puts $!.message, $@.first
    end
    $stderr.reopen(IO::NULL)
    $stdout.reopen(IO::NULL)
  end
end

答案 1 :(得分:0)

如果您使用的是Unix / Linux操作系统,并且从命令行运行测试,则可以

rspec bowling_spec.rb | head -n 20

确保您无需向上滚动即可查看错误。