How to var_dump (debugging) in Ruby on Rails

In the process of app development, we need to debug our application frequently to trace the data flow is correct as expected. I am new in Ruby on Rails and have a trouble about how to debug my code in Ruby on Rails.

Previously i used to use var_dump() or dd() in PHP language. So how to implements such things in Ruby on Rails. Finally I got the answer, it is so simple tho.

Debugging from views is simple:

<%= debug @variable %>

But debugging from inside a controller or model requires you to either halt the execution with abort, which will output an error page:

abort variable.inspect

Or you can write to the rails log with:

logger.debug(@variable.inspect)

You can read the log by using tail -f /logs/development.log from the your shell.

P.S. My favourite the second one, because of it’s simplicity. I can get the data value without need to check the log file.

Source: https://stackoverflow.com/questions/31109639/var-dump-and-die-like-php-in-ruby-on-rails-debug-in-ruby-on-rails

If you want to implement bubble sort in golang you can read this article: https://tech.digitindo.com/2021/04/04/bubble-sort-with-golang/

Leave a Comment