Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reduce the output in a stack trace?

Tags:

ruby

Is there any way to reduce the amount of output provided by Ruby when there's an error?

For example:

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'

All I'm interested in is the first line, cannot load such file -- bowling (LoadError). Ideally I'd like Ruby to not spit out the rest of the from lines.

Is this possible?

like image 213
Snowcrash Avatar asked Nov 02 '22 18:11

Snowcrash


1 Answers

Do something like this:

module Kernel
  at_exit do
    case $!
    when nil, SystemExit, Interrupt
    else puts $!.message, [email protected]
    end
    $stderr.reopen(IO::NULL)
    $stdout.reopen(IO::NULL)
  end
end
like image 84
sawa Avatar answered Nov 09 '22 10:11

sawa