Mind Dump, Tech And Life Blog
written by Ivan Alenko
published under license CC4-BY
posted at 04. May '24

Debugging Ruby and Ruby on Rails Cheatsheet

Basic

puts

You can put puts or if it doesn’t work, Rails.logger, almost everywhere (except some very weird DSL gems) - controllers, views (<% puts "blabla" %>), jobs.

binding.pry

The same, stops execution and provides command line. Pain to use to debug controllers in Docker, but still useful for tests.

More Advanced

caller

In a prompt or anywhere in a method add caller and it provides backtrace as an array.

irb(main):003> caller
=>
["/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.12.0/lib/irb/workspace.rb:117:in `eval'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.12.0/lib/irb/workspace.rb:117:in `evaluate'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.12.0/lib/irb/context.rb:609:in `evaluate'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.12.0/lib/irb.rb:1012:in `block (2 levels) in eval_input'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.12.0/lib/irb.rb:1323:in `signal_status'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.12.0/lib/irb.rb:1005:in `block in eval_input'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.12.0/lib/irb.rb:1085:in `block in each_top_level_statement'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.12.0/lib/irb.rb:1082:in `loop'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.12.0/lib/irb.rb:1082:in `each_top_level_statement'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.12.0/lib/irb.rb:1004:in `eval_input'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.12.0/lib/irb.rb:988:in `block in run'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.12.0/lib/irb.rb:987:in `catch'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.12.0/lib/irb.rb:987:in `run'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.12.0/lib/irb.rb:884:in `start'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3.2/lib/rails/commands/console/console_command.rb:78:in `start'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3.2/lib/rails/commands/console/console_command.rb:16:in `start'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3.2/lib/rails/commands/console/console_command.rb:106:in `perform'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/thor-1.3.1/lib/thor/command.rb:28:in `run'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/thor-1.3.1/lib/thor/invocation.rb:127:in `invoke_command'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3.2/lib/rails/command/base.rb:178:in `invoke_command'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/thor-1.3.1/lib/thor.rb:527:in `dispatch'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3.2/lib/rails/command/base.rb:73:in `perform'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3.2/lib/rails/command.rb:71:in `block in invoke'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3.2/lib/rails/command.rb:149:in `with_argv'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3.2/lib/rails/command.rb:69:in `invoke'",
 "/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3.2/lib/rails/commands.rb:18:in `<top (required)>'",
 "<internal:/home/damon/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'",
 "<internal:/home/damon/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'",
 "bin/rails:4:in `<main>'"]

source_location

Find out where the method is defined exactly

OniSorceress::Content::Article.method(:find).source_location
=> ["/home/damon/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/activerecord-7.1.3.2/lib/active_record/core.rb", 243]

Returns nil for methods defined in C like String#to_s.

owner of method

Find out which module provides the method

"damon".method(:to_s).owner
=> String

Add Comment