Definitely useful! Debug code for development in Ruby on Rails

What is this, what is this

Hello.

I think that the most required technology for development is debugging ability. Especially for me who is good at mass-producing bugs, debugging ability is very important.

Therefore, I would like to put together the debugging code that I usually use in development.

Debug code

I will write a list of code that can be used in Ruby on Rails and may be useful for debugging. There is also code that can be used in Ruby.

It is supposed to be used when starting byebug or with rails console. If you are not sure how to use byebug, please use it in the following article. https://web-nari.net/2018/03/16/byebug/

Method list

Returns a list of methods that can be referenced by the variable hoge and whose method name contains method_name.

hoge.methods.select {|name| name.to_s.include?("method_name") }

Method definition location

Returns the class in which the foo method that can be referenced by the variable hoge is defined.

hoge.method(:foo)

Source file path

Returns the path of the file in which the foo method that can be referenced by the variable hoge is defined.

hoge.method(:foo).source_location

Callback search

Returns a list of save callbacks defined in the class Hoge. If after or before is given to the argument of cb.kind.eql?, the callbacks defined in the after_callback function and the before_callback function are returned, respectively.

Hoge._save_callbacks.select { |cb| cb.kind.eql?(:after) }.map { |cb| cb.filter }

Other callback functions

In addition to save, the following callback functions can be used respectively.

=> [:validate,
 :validation,
 :initialize,
 :find,
 :touch,
 :save,
 :create,
 :update,
 :destroy,
 :commit,
 :rollback,
 :before_commit,
 :before_commit_without_transaction_enrollment,
 :commit_without_transaction_enrollment,
 :rollback_without_transaction_enrollment]

For example, if you want to know the list of after_validation callbacks, do as follows.

Hoge._validation_callbacks.select { |cb| cb.kind.eql?(:after) }.map { |cb| cb.filter }

Classification

Converts the name : string to the appropriate class. In the case of the example, String is returned. A NameError: uninitialized constant is raised if there is no such class.

:string.to_s.classify.constantize

Class confirmation

Check the class of the object named hoge.

hoge.class

Search for inheritance source

You can also search for inherited classes of the object hoge and their ancestors.

hoge.superclass #Find parent class
hoge.ancestors #Search for ancestors

Check instance variables

Returns the instance variable that the object named hoge has. Class variables can be referenced with class_variables.

hoge.instance_variables

Summary

When I wrote it for the momentum of the title, it was very few.

We will continue to add more.

Recommended Posts

Definitely useful! Debug code for development in Ruby on Rails
[Ruby on Rails] Debug (binding.pry)
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
Ruby on Rails Japanese-English support i18n
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] About bundler (for beginners)
Ruby on Rails in Visual Studio Codespaces
Explanation of Ruby on rails for beginners ①
Beginners create portfolio in Ruby on Rails
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
How to debug the processing in the Ruby on Rails model only on the console
Validation settings for Ruby on Rails login function
Recommendation of Service class in Ruby on Rails
[Ruby on Rails] Select2 introduction memo for Webpacker
Rails new in Ruby on Rails ~ Memorandum until deployment 2
Rails new in Ruby on Rails ~ Memorandum until deployment 1
[Rails] Procedure for linking databases with Ruby On Rails
(Ruby on Rails6) Creating data in a table
Ruby on Rails development environment construction on M1 Mac
[Ruby on Rails] How to install Bootstrap in Rails
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Environment construction] Ruby on Rails 5.2 system development environment construction [within 1 hour]
[Ruby on Rails] How to write enum in Japanese
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Build a Ruby on Rails development environment on AWS Cloud9
Docker the development environment of Ruby on Rails project
[Ruby On Rails] How to reset DB in Heroku
[Ruby on Rails] Post image preview function in refile
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Ruby on rails learning record -2020.10.03
[Rails] Settings for loading web fonts in CloudFront on Heroku
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails for beginners! !! Summary of new posting functions
[Ruby on Rails] Quickly display the page title in the browser
Ruby on Rails basic learning ①
<Dot installation> Introduction to Ruby on Rails5 Source code comparison
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
[Environment construction] Get the Ruby on Rails 6 development environment within 1 hour
[Ruby on Rails] Automatically enter the address from the zip code
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
How to display a graph in Ruby on Rails (LazyHighChart)
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Ruby on rails learning record -2020.10.06
Apply CSS to a specific View in Ruby on Rails
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
[For beginners] Build the environment for Ruby on Rails Tutorial 4th Edition (Rails 5.1) in less than an hour!