[RUBY] From introduction to usage of byebug

Do you use debugging tools when studying Rails? A debug tool is a tool that supports the debugging work that is inherent in programming. Step execution is to execute a program line by line and check the operation while looking at the contents of the variables declared at that time. Can be done.

Let's introduce byebug used for Rails development.

First of all, if you install the byebug gem and put a method called "byebug" in the code you want to debug, it will stop when the program passes there, debug mode will start, and you can freely type commands interactively. You can execute and check the value of the variable.

Installation method

Install on Gemfaile

gem 'byebug'

Just add. (If you have rails installed, it is installed by default)

After adding

bundle install

I will install the gem with.

How to use byebug

To start byebug, write a method called byebug where you want to debug. (This place is an example.)

スクリーンショット 2020-09-03 10.44.03.png

This time I will insert byebug in the static_pages / home page part.

After inserting byebug, try hitting the method.

スクリーンショット 2020-09-03 10.45.14.png

In this way, the process will stop and the input prompt will appear. If you type next and press Enter

スクリーンショット 2020-09-03 10.46.37.png

Moved to the next line. Now if you type to look up the help method and press Enter

スクリーンショット 2020-09-03 10.48.24.png

You can check the contents of help. (You can also see the contents of the variables) In the actual debugging work, try executing the variable values and methods in this way to check if they are working as expected.

List of frequently used commands

next Go one line forward
continue Go to the next breakpoint
Step inside the step method
list View source code
up View above the source code
down View below the source code

If there are other commands you want to use, enter help and it will be displayed, so please check it ♪

Once you remember this far, I think the quickest way to get used to it is to actually write it.

Summary

I introduced byebug, but how was it? Byebug also allows you to see the types of each variable, so you can see the details of programming that tend to be black-boxed, speeding up development and two birds with one stone.

I think it's a very useful tool once you get used to it, so please try it ♪

Since I am a fledgling engineer, I think there are some mistakes and points that are difficult to understand. In that case, if you can add comments etc., it will lead to future growth, so please do not hesitate to contact us. If you press [LGTM], you will be happy to jump. Even Twitte is muttering, so please come and visit us if you like ♪

Recommended Posts

From introduction to usage of byebug
From introduction to use of ActiveHash
From setup to usage of Selenium wrapper Selenide
From the introduction of devise to the creation of the users table
Introduction to Ruby (from other languages)
From pulling docker-image of rails to launching
Output of the book "Introduction to Java"
Introduction to monitoring from Java Touching Prometheus
Introduction and usage explanation of Font Awesome
Introduction to Ruby 2
[Java] Flow from introduction of STS to confirmation of dynamic page on localhost (2/3)
Introduction of pay.jp
Introduction to SWING
[Java] Flow from introduction of STS to confirmation of dynamic page on localhost (1/3)
Introduction of milkode
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to migration
Introduction to java
Introduction to Doma
[Docker] Introduction to docker compose Basic summary of docker-compose.yml
Introduction and basic usage of Simple Calendar gem
Summary of moss when updating from JMockit 1.4 to 1.30
How to use enum (introduction of Japanese notation)
[Introduction to Java] Basics of java arithmetic (for beginners)
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Introduction to Ratpack (8)-Session
How to write Scala from the perspective of Java
Migrate from JUnit 4 to JUnit 5
Introduction to programming for college students (updated from time to time)
Introduction to RSpec 1. Test, RSpec
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
The story of migrating from Paperclip to Active Storage
Understand the characteristics of Scala in 5 minutes (Introduction to Scala)
Minimal usage of Mockito
Introduction to PlayFramework 2.7 ① Overview
Introduction to Android Layout
Introduction to design patterns (introduction)
Introduction to Practical Programming
Introduction to javadoc command
From Java to Ruby !!
Introduction to jar command
[Practice! ] Introduction of JFrame (explaining up to screen creation)
Introduction to Java for beginners Basic knowledge of Java language ①
Introduction to Ratpack (2)-Architecture
[Eclipse] Summary of environment settings * Updated from time to time
How to prevent editTextPreference of android PreferenceFragmentCompat from breaking
Introduction of user authentication
Introduction to lambda expression
[Beginner] How to use devise Change settings from introduction
Introduction to java command
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Introduction to javac command
[Ubuntu20.04] From ROS-noetic installation to SLAM simulation of turtlebot3
The story of raising Spring Boot from 1.5 series to 2.1 series part2
Java to fly data from Android to ROS of Jetson Nano