How to debug ruby code?

后端 未结 9 2074
伪装坚强ぢ
伪装坚强ぢ 2020-12-28 15:04

I run Ubuntu 10.10. I just want to debug a simple script. After spending half a day trying to figure out how that could be done I give up. What the heck am I supposed to do?

相关标签:
9条回答
  • 2020-12-28 16:00

    The best debugger I've ever used for Ruby is the one built in to Netbeans. You have to install the fast ruby debugger gem from Netbeans (I'm not sure which gem it actually is, but Netbeans prompts you to do it). I find that it works much better if you switch Netbeans away from the built-in JRuby 1.4 to your system's default Ruby installation. There's also the breakpoint gem that's worth taking a look at, and using the Ruby built-in library logger from the start of your development is also helpful. Good luck!

    0 讨论(0)
  • 2020-12-28 16:00

    Try default Ruby Debugger either by:

    ruby -r debug filename[, ...] 
    

    Or if it's CLI script, just change its first line from:

    #!/usr/bin/env ruby
    

    to:

    #!/usr/bin/env ruby -rdebug
    

    and the script will stop on each Exception.

    Or check the following script sample:

    #!/usr/bin/env ruby
    class Hello
       def initialize( hello )
          @hello = hello
       end
       def hello
          @hello
       end
    end
    
    salute = Hello.new( "Hello, Mac!" )
    puts salute.hello
    

    You can debug it as shown below:

    # ruby -r debug hello.rb
    Debug.rb
    Emacs support available.
    
    hello.rb:3:class Hello
    (rdb:1) v l
      salute => nil
    (rdb:1) b 10
    Set breakpoint 1 at hello.rb:10
    (rdb:1) c
    Hello, Mac!
    

    Source: Ruby Debugger


    Alternatively use lldb/gdb. See below the simple example to print script backtrace into foreground:

    echo 'call (void)rb_backtrace()' | lldb -p $(pgrep -nf ruby)
    

    Replace lldb with gdb if works better. Prefix with sudo to debug non-owned process.

    0 讨论(0)
  • 2020-12-28 16:02
    1. In Ruby:

      ruby -rdebug myscript.rb then,

      • b : put break-point
      • and n(ext) or s(tep) and c(ontinue)
      • p(uts) for display
    2. In Rails: Launch the server with

      • script/server --debugger

        and add debugger in the code.

    0 讨论(0)
提交回复
热议问题