Debugging Ruby Scripts run from Scrivener

A small tip for others who may want to use Ruby for post-processing from Scrivener and are not experts. Ruby has an excellent debugger called byebug ([b]sudo gem install byebug[/b] to install it, then ‘s’ to step in or ‘n’ to step next and you examine things line by line), and if you run a script directly you simply add this at the top of your script:

require 'byebug'
byebug #we enter the debugger here

BUT, what if you want to debug a script run by another program like scrivener? You can’t access the debugger to enter commands. Byebug has a trick, it can run as a remote, with a web interface another instance of byebug can connect to. In this case you add this to your script to be run by Scrivener:

require 'byebug/core'; require 'byebug'
Byebug.wait_connection = true
Byebug.start_server('', 8989)

When this is run it will wait for another byebug to connect to that port. So in a new terminal you run:

byebug -R localhost:8989

And now you can debug/tweak the remote script that was triggered by Scrivener. Remove that code when you are happy with the new script.

Read more here: … e-programs