The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Poor Man's Breakpoint

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Obie Fernandez

Posts: 608
Nickname: obie
Registered: Aug, 2005

Obie Fernandez is a Technologist for ThoughtWorks
Poor Man's Breakpoint Posted: Mar 4, 2007 4:15 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Obie Fernandez.
Original Post: Poor Man's Breakpoint
Feed Title: Obie On Rails (Has It Been 9 Years Already?)
Feed URL: http://jroller.com/obie/feed/entries/rss
Feed Description: Obie Fernandez talks about life as a technologist, mostly as ramblings about software development and consulting. Nowadays it's pretty much all about Ruby and Ruby on Rails.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Obie Fernandez
Latest Posts From Obie On Rails (Has It Been 9 Years Already?)

Advertisement

My last post, about pausing execution with gets, got me thinking about what would happen if I combined gets with eval. Maybe I could put together a reasonably good fascimile of breakpoint, which I've missed dearly since upgrading to Ruby 1.8.5.

I immediately figured out that you have to pass in the binding (or context) of the code where you want to breakpoint, which frankly is no big deal. I never used the arguments to breakpoint anyway. Without too much trouble, my first attempt worked, albeit crudely.

def breakpoint b
  while !(line = gets).strip.eql? 'exit'
    begin
      puts eval(line, b).inspect
    rescue
      puts $!
      next
    end
  end
end

Then Zed suggested that I use readline instead of gets, so my hack would understand Ctrl-A and have a buffer history. That worked pretty great, and I'm sure someone could take it further and add completion.


require 'readline'
include Readline

def breakpoint b
  while !(line = readline("> ", true)).strip.eql? 'exit'
    begin
      puts eval(line, b).inspect
    rescue
      puts $!
      next
    end
  end
end

Finally, I started thinking that it'd really be nice to just fire up IRB instead of my hackish while/gets/eval routine. Which ends up being a very simple solution indeed.


require 'irb'

def breakpoint b
  puts "Executing breakpoint at " + caller.first
  IRB.start(nil, IRB::WorkSpace.new(b))
end

To use just throw the code into an .rb file in your lib directory and require it at the bottom of your config/environment.rb file. Then type breakpoint binding wherever you want a breakpoint. Note: I only use breakpoints in tests and one-off Ruby scripts. What I've detailed here is no replacement for the DRb-based breakpointer code in Rails.

Read: Poor Man's Breakpoint

Topic: Data Mining Mailing List Archives with DEVONthink Pro Previous Topic   Next Topic Topic: Pause Ruby execution using gets

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use