CSC/ECE 517 Fall 2014/ch1a 26 sn: Difference between revisions
Line 41: | Line 41: | ||
The Step command continues execution by moving into the method on the next line. To put it in another way, the command takes us deeper into the method. Following our example, the step command would take us into 'method3' at which point we would be asked what to do next. | The Step command continues execution by moving into the method on the next line. To put it in another way, the command takes us deeper into the method. Following our example, the step command would take us into 'method3' at which point we would be asked what to do next. | ||
== The Next command == | === The Next command === | ||
The next command runs the current line and moves to the next line in the current context. If the next line in the code is a method call, then unlike the 'step' method the execution does not go deeper into the method. Following our example, if we execute the above code, the execution stop at the 'binding.pry' line and the debugger asks us for the further action. On using the 'next' command, the execution moves to the next line that is ' new_products = method3(seller)' without going deeper into the method. All the code within the 'method3' method (which would otherwise be many steps), has been concluded with a single use of the 'next' command. | |||
== The Finish command == | == The Finish command == | ||
== The Continue command == | == The Continue command == | ||
= Reference = | = Reference = |
Revision as of 01:07, 16 September 2014
Debugging in Rails using Pry
Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a computer program or a piece of electronic hardware, thus making it behave as expected. Few of the debugging techniques are Print debugging (also known as printf debugging), Log debugging and Interactive debugging. Rails provides a range of options to make debugging easier which include debug helpers, loggers, graphical debuggers, Pry, and so on. In the following sections, we would be discussing about debugging in Rails using Pry.
Introduction to Pry debugger
Pry is an interactive shell for the Ruby programming language. It is possible to start Pry at any point inside a running program. Due to the reflective nature of Ruby, this lets the programmer inspect the program, change its current state, or correct the source code without restarting the process. Pry is thus notable for its ability to start a REPL within a running program.
Pry has the following features that make it extremely efficient to use:
- View a document or source for a method: The show-doc method gives you information regarding the method and to take a quick look at the source, there's also the show-method method.
- Search your Pry history: Pry's hist --grep makes it easy to search through your Pry history.
Commands
require 'pry' def method1 (seller) puts 'Inside method 1' method2(seller) end def method2(seller) binding.pry # Execution will stop here. new_products = method3(seller) puts 'After calling method3' end def method3(seller) puts 'Inside method 3' puts 'Continuing in method 3' end method1('xyz')
The Step command
The Step command continues execution by moving into the method on the next line. To put it in another way, the command takes us deeper into the method. Following our example, the step command would take us into 'method3' at which point we would be asked what to do next.
The Next command
The next command runs the current line and moves to the next line in the current context. If the next line in the code is a method call, then unlike the 'step' method the execution does not go deeper into the method. Following our example, if we execute the above code, the execution stop at the 'binding.pry' line and the debugger asks us for the further action. On using the 'next' command, the execution moves to the next line that is ' new_products = method3(seller)' without going deeper into the method. All the code within the 'method3' method (which would otherwise be many steps), has been concluded with a single use of the 'next' command.