How to debug ruby code?

后端 未结 9 2222
伪装坚强ぢ
伪装坚强ぢ 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

    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.

提交回复
热议问题