puts - Prints a string to the console, often used for simple debugging.
Example:
x = 5
puts "=" * 10
puts "Value of x: #{x}" # Output: Value of x: 5
puts "=" * 10
A comprehensive guide to debugging Ruby code using built-in tools, Pry, and Byebug. This cheat sheet covers essential commands, techniques, and best practices to efficiently identify and resolve issues in your Ruby applications.
|
Example:
|
|
Example:
|
|
Example:
|
|
Understanding backtraces is crucial for pinpointing the source of errors. Ruby provides detailed information about the call stack when an exception occurs. Example:
|
|
Analyzing a Backtrace:
|
|
Using Ruby’s built-in Example:
|
|
Log Levels:
|
|
The |
|
Accessing Method Objects:
|
|
Retrieving Method Source Location:
This returns an array containing the file path and line number where the method is defined. |
|
Handling Methods Defined in C: For methods implemented in C,
|
|
Using Within a Pry session,
Inside Pry:
|
|
Inspecting Method Parameters: The
This shows required, optional, rest, and keyword arguments. |
|
Returns an array of strings representing the call stack. Each string describes a single method call, including the file name, line number, and method name. |
|
Basic usage:
|
|
Returns only the |
|
Limiting the output:
|
|
Returns an array of |
|
Using
|
|
Filtering the call stack: You can use |
|
Filtering example:
|
|
Using Insert |
|
Be aware of performance: Avoid using |
|
Stop based on some conditions
or
|
|
If you need to print Hash or JSON in a nice way
You can also use “pp” method or gems like awesome_print. |
|
To start using the Debug gem, first add it to your Gemfile:
Then run |
|
Require the Debug gem in your application with:
|
|
To initiate a debugging session, insert the following line into your code where you want to start debugging:
|
|
Run your Ruby script. Execution will pause at the |
|
Ensure you’re running your Ruby application with |
|
Use
This will print local variables and then continue the program. |
|
Use
This will print local variables and then open the console. |
|
These options help automate common debugging tasks and reduce manual steps. |
|
Moves to the next line within the same context, stepping over method calls. |
|
Executes the next line of code, stepping into any methods on the line. |
|
This will continue running the program until the next breakpoint. |
|
This is useful for quickly skipping over long methods. |
|
Example: |
|
Useful to see the surrounding context. |
|
Example: |
|
|
|
For example, |
|
You can also use |
|
Example: |
|
Stops execution when the specified condition is true. Example: |
|
Use |
|
Example: |
|
Utilize |
|
|
|
|
|
|
|
Activate tracing with specific options as needed. |
|
Example: |
|
Pry is a powerful alternative to Installation:
|
|
To start a Pry session, insert Example:
|
|
|
List variables and methods in the current scope. |
|
|
Change the current context to the given object. |
|
|
Show the current location in the code. |
|
|
Display the source code of a method. |
|
|
Exit the Pry session. |
|
|
Display help information. |
|
Pry supports command aliases, allowing you to create shortcuts for frequently used commands. Example:
Now you can use |
|
Pry integrates well with other debugging tools like |
|
Byebug is a powerful debugger for Ruby that allows you to step through code, set breakpoints, and inspect variables. Installation:
|
|
To start debugging, insert Example:
|
|
|
Execute the next line of code. |
|
|
Step into a method call. |
|
|
Continue execution until the next breakpoint or the end of the program. |
|
|
Set a breakpoint at the specified location (e.g., |
|
|
Display information about the current state. |
|
|
Automatically display the value of an expression each time the debugger stops. |
|
|
Print the value of an expression. |
|
|
Exit the debugger. |
|
Byebug allows you to set breakpoints that are only triggered when a certain condition is met. Example:
|
|
|
|
|
|
|
|
|
|
|
|
A fast, accurate Ruby profiler, providing detailed performance reports for Ruby code. |
Offers call stack, flat, and graph profiles to pinpoint bottlenecks. |
|
A sampling call-stack profiler for Ruby, designed for speed and low overhead. |
Captures stack samples to identify frequently called methods, helping optimize performance-critical sections. |
|
A gem to profile memory usage in Ruby apps, identifying memory leaks and allocations. |
Provides insights into object allocation, retention, and garbage collection behavior, crucial for memory optimization. |
|
A standard library module for benchmarking Ruby code snippets. |
Allows timing of code execution, comparing performance of different approaches, and identifying performance regressions. |
|
A gem to help increase your Rails application’s performance by reducing the number of queries it makes. |
Alerts you to N+1 queries, unused eager loading, and suggests solutions. |
|
A middleware that displays speed badge for every html page, showing overall load time. |
Provides detailed information about request performance, including SQL queries, view rendering, and more, directly in the browser. |
|
A series of things you can use to benchmark different parts of your Rails or Ruby app. |
Includes tools to measure memory usage, object allocations, and garbage collection performance. |
|
My own gem to print values of objects to the console, without typing “puts” or “logger.debug”. |
Prints value of the object without modifying it.
(this |