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
    Debugging and Tracing Tool for Ruby
    http://github.com/eggegg/defly

    View Slide

  2. Andrew Liu 劉彥廷
    • College Student
    • Intern @ Cardinalblue
    • eggegg @ github
    • Ruby, iOS, Android
    [email protected]

    View Slide

  3. gem install defly

    View Slide

  4. Defly is for...
    • Trace method calls
    • Trace instance variables
    • Better error messages
    • Inspect the point of error

    View Slide

  5. class Warrior
    attr_accessor :hp, :mp
    def sleep
    self.hp += 10
    self.mp += 2
    end
    end

    View Slide

  6. 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

    View Slide

  7. 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

    View Slide

  8. 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

    View Slide

  9. 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 `'
    ...
    bug.rb
    irb
    Where is the bug???

    View Slide

  10. 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.<>
    from /Users/andrewliu/bug.rb:2:in `'

    View Slide

  11. 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!

    View Slide

  12. >>>>> Error received:
    "Engine Fail"
    >>>>>
    #> @reason
    => "Bugs invasion"
    #>
    Ruby shell (Rib by godfat)
    to inspect errors!

    View Slide

  13. Thanks!
    Any Question?

    View Slide