Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Defly

 Defly

A debug tool for Ruby

Andrew Liu

August 27, 2011
Tweet

More Decks by Andrew Liu

Other Decks in Programming

Transcript

  1. Defly is for... • Trace method calls • Trace instance

    variables • Better error messages • Inspect the point of error
  2. class Warrior attr_accessor :hp, :mp def sleep puts "BEFORE: #{@hp},

    #{@mp}" self.hp += 10 puts "AFTER ADDING HP: #{@hp}, #{@mp}" self.mp += 2 puts "AFTER ADDING MP: #{@hp}, #{@mp}" end end
  3. class Warrior attr_accessor :hp, :mp def sleep self.hp += 10

    self.mp += 2 end end require 'defly' Warrior.debug! Warrior.new.trace([:hp, :hp=, :mp, :mp=, :sleep], [:@hp, :@mp]) do |warrior| warrior.hp = 10 warrior.mp = 20 warrior.sleep end
  4. Tracing hp, hp=, mp, mp=, sleep on Warrior instance Tracing

    @hp, @mp on Warrior instance <<<<< Warrior#hp=(10) # (irb):14:in `block in irb_binding' @hp = 10 # undefined @mp = nil # undefined >>>>> 10 <<<<< Warrior#mp=(20) # (irb):15:in `block in irb_binding' @mp = 20 # undefined >>>>> 20 <<<<< Warrior#sleep() # (irb):16:in `block in irb_binding' <<<<< Warrior#hp() # (irb):7:in `sleep' >>>>> 10 <<<<< Warrior#hp=(20) # (irb):7:in `sleep' @hp = 20 # 10 -> 20 >>>>> 20 <<<<< Warrior#mp() # (irb):8:in `sleep' >>>>> 20 <<<<< Warrior#mp=(22) # (irb):8:in `sleep' @mp = 22 # 20 -> 22 >>>>> 22 >>>>> 22
  5. NoMethodError debugging = nil debugging.is_annoying irb(main):001:0> require 'bug' NoMethodError: undefined

    method `is_annoying' for nil:NilClass from /Users/eggegg/bug.rb:2:in `<top (required)>' ... bug.rb irb Where is the bug???
  6. irb(main):003:0> require 'defly' => true irb(main):004:0> require 'bug' NoMethodError: undefined

    method `is_annoying' for nil:NilClass bug.rb:2> debugging.<<is_annoying>> from /Users/andrewliu/bug.rb:2:in `<top (required)>'
  7. Inspecting Errors class Rocket def launch! @reason = "Bugs invasion"

    raise "Engine Fail" end end Rocket.debug! rocket = Rocket.new rocket.watch_error "Engine Fail" rocket.launch!
  8. >>>>> Error received: "Engine Fail" >>>>> #<Rocket:0(0)>> @reason => "Bugs

    invasion" #<Rocket:0(0)>> Ruby shell (Rib by godfat) to inspect errors!