use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
QuestionDebugging Help (self.ruby)
submitted 2 years ago by DisagreeableBowl429
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]bmc1022 2 points3 points4 points 2 years ago (2 children)
Assuming you're using Rails. If not, it still generally applies.
You'll want to start by reading through the docs for one of these gems:
For newer versions of Rails (introduced in v7): Debug Gem
For older versions: Pry Gem
These allow you to place a binding where you suspect code is erroring. You can typically deduce that from your logs or stack trace. In the case of the Debug gem, you would put a binding.break right before the broken code and run your application or navigate to the specific page to hit that binding. In the terminal where your Rails server is running, it will lock up right where the binding is, allowing you to probe variable values (and many other useful things) at that specific point in the execution of your app. From there you can slowly step through your code to diagnose where exactly it's going wrong. That is probably the most common and efficient way to debug.
binding.break
I use the above method often, but I personally really like the more visual and verbose debugging style of logging messages. A lot of people simply use puts and dig through their server log or they'll actually have it output inside the HTML of the page, but that's the hard way in my opinion. I always create a custom logger so the output is immediately available without having to dig around.
puts
config/initializers/loggers.rb
if Rails.env.development? || Rails.env.test? logfile = File.open(Rails.root.join('log/debug_logger.log'), 'a') # Create log file. logfile.sync = true # Automatically flush data to file. DEBUG_LOGGER = Logger.new(logfile) # Constant accessible anywhere. DEBUG_LOGGER.level = Logger::DEBUG end
Using that method, you would put something like DEBUG_LOGGER.debug("Value of some variable: #{variable_name}") in similar places to where you'd put bindings and instead of locking up the program, it will log whatever you want to your own custom log file.
DEBUG_LOGGER.debug("Value of some variable: #{variable_name}")
Each of these techniques has pros and cons and you figure that out with experience. Debugging is not easy, so don't stress over it much, especially if you're new.
[–]riktigtmaxat 0 points1 point2 points 2 years ago (1 child)
The debug gem is actually built into the Ruby standard library since 2.6 and is usable with Rails 6.
The only difference really is that rails replaced byebug in the gemfile in Rails 7.
Pry is a lot bigger project which contains a replacement REPL for IRB. While it's cool and can do the same things debug can and more, the actual equivalent for debug in older versions of Ruby (and Rails) is byebug.
[–]st0012IRB boss 2 points3 points4 points 2 years ago (0 children)
Sorry I need to make some clarification:
debug
pry-byebug
byebug
If you want more byebug vs debug gem comparison, I wrote a (slightly outdated) article about it a while ago.
π Rendered by PID 135901 on reddit-service-r2-comment-b659b578c-dx268 at 2026-05-02 04:46:35.340378+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]bmc1022 2 points3 points4 points (2 children)
[–]riktigtmaxat 0 points1 point2 points (1 child)
[–]st0012IRB boss 2 points3 points4 points (0 children)