Debugging, Linting and Code Quality
43.1 Debugging
Alternatives: pry / pry-byebug (popular REPL debugger). VS Code and RubyMine have integrated debugging. error_highlight (3.1+) shows exactly where an error happened in the line.
require "debug" # the default debugger (Ruby 3.1+)
binding.break # breakpoint (also: debugger)
# commands: n(ext), s(tep), c(ontinue), info, bt (backtrace), p expr, watch @var, break file.rb:10, finish
binding.irb # drop into an IRB session at this point
p obj # quick inspection
pp complex_structure
puts caller.first(5) # call stackrdbg app.rb # run under the debugger
rdbg -c -- bundle exec rspec # debug a command
ruby -r debug/open app.rb # remote debugging (VS Code attaches)43.2 Linting and Formatting
Standard (standardrb): a zero-config RuboCop wrapper. Ruby LSP formats with RuboCop or Syntax Tree.
Quality tools: Reek (code smells), Flog/Flay (complexity/duplication), RubyCritic (reports), Brakeman (Rails security), bundler-audit (vulnerable gems), SimpleCov (coverage), YARD (documentation).
gem install rubocop rubocop-performance rubocop-rspec
rubocop # check
rubocop -a # safe autocorrect
rubocop -A # all autocorrect
rubocop --auto-gen-config # create .rubocop_todo.yml for legacy code# .rubocop.yml
AllCops:
TargetRubyVersion: 3.3
NewCops: enable
plugins:
- rubocop-performance
- rubocop-rspec
Style/StringLiterals:
EnforcedStyle: double_quotes
Metrics/MethodLength:
Max: 15