How to Start the Ruby Debugger on Exception

Is there any way to start the Ruby debugger on exception?

I stumbled across this page:post-mortem debugging. Doing this:

Debugger.start(:post_mortem => true)

gets me where I want to.

Start ruby debugger if rspec test fails

Use pry-rescue, it's the spiritual successor to plymouth:

From the Readme:

If you're using RSpec or respec, you can open a pry session on every test failure using rescue rspec or rescue respec:

$ rescue rspec
From: /home/conrad/0/ruby/pry-rescue/examples/example_spec.rb @ line 9 :

6:
7: describe "Float" do
8: it "should be able to add" do
=> 9: (0.1 + 0.2).should == 0.3
10: end
11: end

RSpec::Expectations::ExpectationNotMetError: expected: 0.3
got: 0.30000000000000004 (using ==)
[1] pry(main)>

How to debug Ruby scripts

Use Pry (GitHub).

Install via:

$ gem install pry
$ pry

Then add:

require 'pry'; binding.pry

into your program.

As of pry 0.12.2 however, there are no navigation commands such as next, break, etc. Some other gems additionally provide this, see for example pry-byebug.

Aptana 3 ruby debugger - Exception in DebugThread loop: undefined method `is_binary_data?'

My ruby version:

ruby 1.9.3p0 (2011-10-30) [i386-mingw32]

My gems list:

...
linecache19 (0.5.13)
ruby-debug-base19 (0.11.26)
ruby-debug-ide19 (0.4.12)
...

In Aptana 3, I got same error.

Exception in DebugThread loop: undefined method `is_binary_data?' for "#<PostsController:0x65a8da8>":String

See ruby-debug-ide19-0.4.12 / xml_printer.rb .

  value_str = "[Binary Data]" if value_str.is_binary_data?
print("<variable name=\"%s\" kind=\"%s\" value=\"%s\" type=\"%s\" hasChildren=\"%s\" objectId=\"%#+x\"/>",
CGI.escapeHTML(name), kind, CGI.escapeHTML(value_str), value.class,
has_children, value.respond_to?(:object_id) ? value.object_id : value.id)

See http://apidock.com/ruby/String/is_binary_data%3F .

String#is_binary_data?

This method is deprecated or moved on the latest stable version. The last existing version (v1_9_1_378) is shown here.

 def is_binary_data?
( self.count( "^ -~", "^\r\n" ).fdiv(self.size) > 0.3 || self.index( "\x00" ) ) unless empty?
end

Add this code to xml_printer.rb (or to your code).

class String
def is_binary_data?
( self.count( "^ -~", "^\r\n" ).fdiv(self.size) > 0.3 || self.index( "\x00" ) ) unless empty?
end
end

Thank you.

Using Ruby with Rubymine, or another debugger, is it possible to enter debug mode upon an error condition?

If I understand you correctly, you need program to stop when exception occurs. You should add an exception breakpoint

How can I start Pry in Rails/Ruby automatically when any exception occurs, at the location of the raised exception

Use pry-rescue it does exactly what you want.



Related Topics



Leave a reply



Submit