How to debug ruby code?

后端 未结 9 2064
伪装坚强ぢ
伪装坚强ぢ 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 15:43

    Use IRB. It is an interactive Ruby shell. When errors occur, it gives a trace with line numbers so that you can tell what part of your code went wrong. You can load your source files and run individual methods to see if they are working properly. IRB provides useful output - when you enter some code, it will evaluate the expression and then print the return value using .inspect.

    0 讨论(0)
  • 2020-12-28 15:44

    Ruby debugging has a difficult history, littered with tools that only support a specific minor version of MRI. Thankfully, for 2.0 and onward, you can use byebug.

    Command-line usage is simple: run byebug <your script>. You can also edit your file and drop the byebug function call in wherever you want to launch debugging.

    0 讨论(0)
  • 2020-12-28 15:47

    There is plenty of console-based debuggers for ruby with different features, based on which you will make choice.

    If you important need - is intuitive navigation (for example next moves line-by-line, stepping into blocks), and quick comprehensible documentation - this works for me the best:

    https://github.com/garmoshka-mo/pry-moves


    Other cool feature which I had demand - is to debug from inside of debugger. Like if I stopped in Controller's action:

    def index
        list = Orders.for_user(current_user) 
    =>  binding.pry
    end
    

    Now I want to understand why list is empty? - I can run debug Orders.for_user(current_user) and see what happens there

    0 讨论(0)
  • 2020-12-28 15:53

    pry is better compared to IRB. The following are grab from its README.

    Pry is a powerful alternative to the standard IRB shell for Ruby. It is written from scratch to provide a number of advanced features, including:

    • Source code browsing (including core C source with the pry-doc gem)
    • Documentation browsing
    • Live help system
    • Open methods in editors (edit-method Class#method)
    • Syntax highlighting
    • Command shell integration (start editors, run git, and rake from within Pry)
    • Gist integration
    • Navigation around state (cd, ls and friends)
    • Runtime invocation (use Pry as a developer console or debugger)
    • Exotic object support (BasicObject instances, IClasses, ...)
    • A Powerful and flexible command system
    • Ability to view and replay history

    • Many convenience commands inspired by IPython, Smalltalk and other advanced REPLs

    • A wide-range number of plugins that provide remote sessions, full debugging functionality, and more.

    Pry also aims to be more than an IRB replacement; it is an attempt to bring REPL driven programming to the Ruby language. It is currently not as powerful as tools like SLIME for lisp, but that is the general direction Pry is heading.

    Pry is also fairly flexible and allows significant user customization is trivial to set it to read from any object that has a readline method and write to any object that has a puts method - many other aspects of Pry are also configurable making it a good choice for implementing custom shells.

    0 讨论(0)
  • 2020-12-28 15:56

    You can see the Cheat sheet running

      gem install cheat
      cheat rdebug
    

    This will show useful commands to use rdebug.

    0 讨论(0)
  • 2020-12-28 15:58

    Ruby-debug is for 1.8+ and ruby-debug19 is for 1.9+.

    ruby-debug is easy to learn and very useful. You can tell the application to run until a certain condition exists, then have it break, making it easy to locate nil values, or other conditions that occur sporadically.

    From the command-line use rdebug appname and you'll end up at the debugger prompt. If you want to run to line 100 and stop you can enter c 100 and the debugger will set a temporary break-point, the program will run then stop there if it's in the execution path. Once it stops the temporary break-point will be cleared. If you always want to stop at line 100 you could do b 100 then c and the debugger will set a permanent break-point, continue, then stop when the break-point is reached. You can clear the breakpoints, set conditional ones that occur when certain conditions apply, etc. You can type n to step to the next instruction skipping over subroutine calls, or s to step into them. There are commands to display contents of variables in various ways, so read through the docs.

    From inside rdebug you can drop into an IRB shell with your variables already populated so you can poke at things to see what happens. From inside either you can inspect or set values, helping with what-if adjustments. If you do that from within rdebug you can continue the program with the altered value(s) and see how it behaves.

    IRB has its place, and it's great for trying things, but it's not a substitute for the debugger, just as the debugger can do some IRB-ish things, but won't replace it. Both tools are a good combination and beat the heck out of relying on print statements or dumping to a log file.


    Pry has emerged as a great combination of IRB and a debugger, and is well worth investigating.

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