Ruby Debuggers

Rocky Bernstein: rocky@gnu.org
slides:
http://rocky.github.io/Bandung-2012
text:
http://github.com/rocky/rocky.github.io/wiki/Bandung-2012-talk
github:
http://github.com/rocky

Second Trip to Java!

I love it!

First trip to find my long-lost brother

I found him, we took photo....

First Trip to Java

When a Debugger?

Some people don't know what a debugger is.

These books don't say much about debuggers...

A Story

  • Monday start a new job. YAY!
  • Job has computer already set up; there is a nice plant on desk...
    because it belongs to a guy who was fired Friday
  • Boss says fix website code written by this guy
  • It is written in Rails4
  • I don't know Rails4 :-(

http://localhost:3000/posts

Rails log:


Started GET "/posts" for 127.0.0.1 at 2012-06-19 11:32:16 -0400
  Processing by PostsController#index as HTML
  Post Load (0.4ms)  SELECT "posts".* FROM "posts"
Rendered posts/index.html.erb within layouts/application (11.9ms)
	  

Rails code posts/index.html.erb


<% @posts.each do |post| %>
  <tr>
...
    <td><%= post.user %></div>
    <td><%= post.text %>
    <td><%= link_to 'Show', post %>
	  

Rails Console


$ rails console
Loading development environment (Rails 3.0.9)
:001 > post = Post.find(:first)
 => #<Post id: 1, user: "rocky", text: "hallo semua!"...>
:002 > post.text
 => "hallo semua!"
:003 > link_to('Show', post)
NoMethodError: undefined method `link_to' for main:Object
	  

Adding debugger call from code


<% @posts.each do |post| %>
  <tr>
...
    <td><%= post.user %></div>
    <td><%= post.text %>
    <td><%= debugger; link_to ... %>
	  

And link_to can be found

and stepped into: show this

Novel ruby-debug commands

Some of these originated with ruby-debug

  • set autoeval on
  • display
  • set different on
  • set autolist on
  • debugger if...
  • --post-mortem

trepanning debugger features

Note: if you have ruby-debug installed you can also install trepan8

  • Syntax Highlighting -- tt>set hightlight
  • Debugger command ...
    completion (se => set, server)
  • eval and eval?
  • Better handling of eval, and return
  • Low-level stuff: (disassembly, stepping by VM instruction)

A debugger doesn't replace...

  • Writing Tests (rspec, test/unit, cucumber)
  • Writing Documentation for your code (rdoc)
  • Writing Modular Code

Terima Kasih