Skip to content

Instantly share code, notes, and snippets.

@jeremyf
Last active February 7, 2018 19:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyf/5237906 to your computer and use it in GitHub Desktop.
Save jeremyf/5237906 to your computer and use it in GitHub Desktop.
Tools for Troubleshooting Ruby

Tools for Troubleshooting Ruby

debugger

https://github.com/cldwalker/debugger

gem install debugger

In ~/.rdebugrc (You'll want to set these)

set autolist
set autoeval
set autoreload

In the location you want to debug, add the following line:

require 'debugger'; debugger; puts 'stay';

(The puts 'stay' is not technically required, but is useful if you are placing the debugger at the end of a block.)

While debugging, or in your IRB/Rails console, the following methods may be helpful:

object.instance_variables
object.instance_variable_get("@name_of_variable")
Class.constants
object.methods.grep(/regexp_for_method/)
object.method(:method_name).source_location # Ruby 1.9

method_locator

https://github.com/ryanlecompte/method_locator

gem install method_locator

object.methods_for(:method_name)

An array of each defintion for that object's method.

object.methods_for(:method_name).each {|m| puts "#{m.source_location.first}:#{m.source_location.last}" }

The above prints the location of all method definitions for the given object's method.

object.method_lookup_path

An array of all of the classes and modules that compose the object.

bundle

Where is the gem I am using located on my machine $ bundle show <gemname>

Open the gem in my editor $ bundle open <gemname>

https://blog.engineyard.com/2013/bundler-hacking

gem

$ gem server or $ bundle exec gem server

Go to http://0.0.0.0:8808/ and view all of the gems, their locations and descriptions. If you installed the gems with RDoc, then you will also have access to the RDocs.

git

I hope you are using git.

Find when an error was introduced: $ git bisect

Have a test script that exits status 0 on success; and not 0 on failure

  • This could be a test script in your project or an external script

Mark one commit as good and another as bad, then run bisect.

  • It will point out exactly which commit broke your test script
  • As an aside, keep your git commits as small as possible, with each commit being a single concern.

http://www.askbjoernhansen.com/2010/04/30/git_bisect_mini_tutorial.html

A demo that you run from command line, walking through a handful of git commands, performing actions on a repo it creates:

https://github.com/jeremyf/git-demo

awesome_print

https://github.com/michaeldv/awesome_print

You may appreciate formatted output in IRB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment