SlideShare a Scribd company logo
Debugging Rails




Michael Denomy
Boston Ruby Project Night
March 5, 2013
Agenda
• Introduction to Rails Debugging
 - Role of testing
 - Examining the log files
 - Interpreting the stack trace
 - The better_errors gem
 - Rails console
 - Using pry gem to debug
Testing
• First line of defense
   - Avoid defects in the first place
• Since adopting TDD, time spent
     ‣ using a debugger
     ‣ writing log messages
     ‣ triaging defects

have all dropped significantly
• Find a defect?
   - First step: write a test to reproduce it
General Debugging Tips
• Reproduce the problem
 - Under what conditions does it occur
   ‣ data, configuration, environment

 - Write a unit/integration test if possible
• Divide and Conquer
 - Reduce the places you have to look
   ‣ Logging statements
   ‣ Simplest conditions to reproduce
   ‣ Any recent changes?
General Debugging Tips
• Pay attention to the data
 - Don’t jump to conclusions
 - Avoid “that can’t be the problem”
• Be methodical
 - Talk the problem out
   ‣ Use “rubber ducky debugging” if solo

 - Change one thing at a time
 - Keep track of changes/ideas you’ve tried out
 - Periodically circle back and re-evaluate
Rails Log Files
• Rails keeps log file for each environment
  - e.g. development.log, production.log
• Log messages have severity levels
  - :debug, :info, :warn, :error, :fatal
  - config.log.level filters what gets logged
• Writing your own log messages
Asset Pipeline Log Messages
• Lots of messages related to asset pipeline
• quiet_assets gem
  - suppresses asset pipeline messages
  - should be used in development only
• thin web server
  - fixes a problem with message that shows up
    with quiet_assets gem and WEBrick

• Gemfile
Reading A Stack Trace
The better_errors gem
• Richer error page
   ‣ Full stack trace
   ‣ Source code inspection for all stack frames
   ‣ Local and instance variable inspection
   ‣ Live REPL on every stack frame

• Advanced features requires
  ‘binding_of_caller’ gem
• Gemfile - Development only!
Using the Rails Console
• Great playground to experiment
  - Especially handy for model access
• Danger!!!
  - Good idea to run in sandbox to prevent
    changing data
      rails console --sandbox
pry gem
• pry is an IRB replacement
  - live help
  - syntax highlighting
  - navigate scope
  - and much, much, more
• pry also supports plugins...lots of them
  - check out www.rubygems.org for full list
• pry-debugger gem
  - step, next, continue
Debug Using an IDE
• If you come from a background where
  you have used an IDE, e.g. Java or C#, an
  IDE is an option
• RubyMine has an integrated debugger
  - Does not play well with pry-debug or other
    debuggers
Summary
• Divide and Conquer Strategy
• Be methodical
• Check the logs for clues
• Testing is the best defense
• Questions?
References
http://gipsysoft.com/articles/debug-rules/
http://pryrepl.org/screencasts.html
http://railscasts.com/episodes/86-logging-variables
http://railscasts.com/episodes/24-the-stack-trace
http://railscasts.com/episodes/280-pry-with-rails
http://samuelmullen.com/2012/07/getting-more-out-of-the-rails-console/
Debugging Rails




Michael Denomy
Boston Ruby Project Night
March 5, 2013

More Related Content

PDF
10 Things you should know about Ruby
PDF
Alexander Kutsan "Where Unicorns Live or How To Write Good C++ Code"
KEY
CPAN Gems From The Far East
PPTX
Docker for everything
PPT
ruby + websocket + haproxy
PDF
Can you TDD Rails?
PPT
Next generation frontend tooling
PPTX
Rails automatic test driven development
10 Things you should know about Ruby
Alexander Kutsan "Where Unicorns Live or How To Write Good C++ Code"
CPAN Gems From The Far East
Docker for everything
ruby + websocket + haproxy
Can you TDD Rails?
Next generation frontend tooling
Rails automatic test driven development

What's hot (20)

PDF
Git hooks For PHP Developers
PPTX
BAM experiences in large scale deployments
PPTX
6 reasons you should program in go
PPTX
Saving Time By Testing With Jest
PDF
JRuby Hot Topics 2008-12-12
PDF
Wordpress -> Middleman: Lesson learned in the 2-years since migrating
PDF
Unit Testing in JavaScript
KEY
Php|tek '12 It's More Than Just Style
PDF
CPAN Workshop, Chicago 2014
PPTX
JRuby deployments
PDF
Indexing BackPAN
PDF
Reason React
PDF
Upgrading to rails3
PDF
2013 - Igor Sysoev - NGINx: origen, evolución y futuro - PHP Conference Argen...
PPTX
Webforms
PDF
What python can learn from java
KEY
Presentacion minitest
PDF
JSFoo-2017 Takeaways
KEY
Ruby Midwest 2010 jRuby by Charles Nutter
PDF
Realtime MVC with Sails.js
Git hooks For PHP Developers
BAM experiences in large scale deployments
6 reasons you should program in go
Saving Time By Testing With Jest
JRuby Hot Topics 2008-12-12
Wordpress -> Middleman: Lesson learned in the 2-years since migrating
Unit Testing in JavaScript
Php|tek '12 It's More Than Just Style
CPAN Workshop, Chicago 2014
JRuby deployments
Indexing BackPAN
Reason React
Upgrading to rails3
2013 - Igor Sysoev - NGINx: origen, evolución y futuro - PHP Conference Argen...
Webforms
What python can learn from java
Presentacion minitest
JSFoo-2017 Takeaways
Ruby Midwest 2010 jRuby by Charles Nutter
Realtime MVC with Sails.js
Ad

Viewers also liked (6)

ODP
Managing the logs of your (Rails) applications - RailsWayCon 2011
PDF
Ruby on Rails for beginners
DOC
Final project report format
PDF
Ruby on Rails Presentation
PPT
Project Report Format
Managing the logs of your (Rails) applications - RailsWayCon 2011
Ruby on Rails for beginners
Final project report format
Ruby on Rails Presentation
Project Report Format
Ad

Similar to Debugging rails (20)

ODP
Debugging Rails 3 Applications
PPTX
Exploring Ruby on Rails and PostgreSQL
PDF
Lessons Learnt in 2009
PPTX
Fastest Servlets in the West
PPT
Performance optimization - JavaScript
PDF
Ruby performance - The low hanging fruit
PPTX
What's the "right" PHP Framework?
PDF
rubyonrails
PDF
rubyonrails
PPTX
Austin Python Learners Meetup - Everything you need to know about programming...
PPTX
Node.js Getting Started &amd Best Practices
PDF
How DSL works on Ruby
PDF
JavaOne 2010: Top 10 Causes for Java Issues in Production and What to Do When...
PDF
2019 PHP Serbia - Boosting your performance with Blackfire
PDF
Code for Startup MVP (Ruby on Rails) Session 1
PDF
Working With Concurrency In Java 8
PPTX
Automated Testing with Cucumber, PhantomJS and Selenium
PDF
Tips For Maintaining OSS Projects
PPTX
Back to Basics Webinar 6: Production Deployment
KEY
Contributing to rails
Debugging Rails 3 Applications
Exploring Ruby on Rails and PostgreSQL
Lessons Learnt in 2009
Fastest Servlets in the West
Performance optimization - JavaScript
Ruby performance - The low hanging fruit
What's the "right" PHP Framework?
rubyonrails
rubyonrails
Austin Python Learners Meetup - Everything you need to know about programming...
Node.js Getting Started &amd Best Practices
How DSL works on Ruby
JavaOne 2010: Top 10 Causes for Java Issues in Production and What to Do When...
2019 PHP Serbia - Boosting your performance with Blackfire
Code for Startup MVP (Ruby on Rails) Session 1
Working With Concurrency In Java 8
Automated Testing with Cucumber, PhantomJS and Selenium
Tips For Maintaining OSS Projects
Back to Basics Webinar 6: Production Deployment
Contributing to rails

More from Michael Denomy (9)

PDF
Intro to automated testing
PPTX
Breathe In and Out With Me - Burlington Ruby Conference 2015
PDF
Writing Maintainable Tests with PageObjects
PDF
Memory Management in RubyMotion
PDF
Aikido, Controller Tests, and LinkedIn
PDF
Reinventing Yourself
PDF
Design Principles - Michael Denomy at Launch Academy
PDF
From Ruby on Rails to RubyMotion - Writing your First iOS App with RubyMotion
PPT
Introduction to Test Driven Development
Intro to automated testing
Breathe In and Out With Me - Burlington Ruby Conference 2015
Writing Maintainable Tests with PageObjects
Memory Management in RubyMotion
Aikido, Controller Tests, and LinkedIn
Reinventing Yourself
Design Principles - Michael Denomy at Launch Academy
From Ruby on Rails to RubyMotion - Writing your First iOS App with RubyMotion
Introduction to Test Driven Development

Debugging rails