Brekpointer

Breakpoints can be very useful, but You have to know how to use it. I’m learning it all the time, and I wish I had more time to do Ruby development. Having for it few hours a week is definitely to low.

Now I give You few tips I’ve learned today…

Output of any command like puts goes to WEBricks log, not to browser neither to irb shell. To see something from such output use client object (think about it as irb-console-client). Like client.puts "Hello!". So we can use it to print variables in a little more friendly way. Lets examine source_lines which is array with current source code. Typing source_lines in irb shell result in something not very readable.

source_lines => ["class ScansController < ApplicationController", " def merge", " breakpoint", " end", "", " def index", " list", " render :action => 'list'"]

So we can write it in more friendly way:

003:0> source_lines.each {|X| client.puts X}

And output will be like:

class ScansController < ApplicationController
  def merge
    breakpoint
  end

  def index
    list
    render :action => 'list'
=> ["class ScansController < ApplicationController", "  def merge", 
"    breakpoint", "  end", "", "  def index", "    list", 
"    render :action => 'list'"]

Nice. But Ruby has class Pretty Print to deal with arrays, hashes and any mix of them. So first You have to load it (client.require 'pp' – remember, You want to output to irb console, so client has be pp-aware ;)) ) and then use it:

irb(#):004:0> client.pp source_lines
["class ScansController < ApplicationController",
 "  def merge",
 "    breakpoint",
 "  end",
 "",
 "  def index",
 "    list",
 "    render :action => 'list'"]
=> nil

Looks even better. In order to avoid typing client.require 'pp' every time execution stops in breakpoint, add to ~/.irbrc file require 'pp', and ./script/brekpointer will load pp every time.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.