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 full-size slide

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

    View full-size slide

  3. gem install defly

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size slide

  13. Thanks!
    Any Question?

    View full-size slide