SlideShare a Scribd company logo
1 of 22
Or:
How
I
Learned
To
Stop
Putsing
And
Love
Pry
DEBUGGING RUBY
ABOUT ME
Luke Bergen
Twitter: @lbergen
Reviewed.com
Developers spend 60% of their time
debugging
puts doesn‟t always do the trick
Enter pry and friends
DEBUGGING IN RUBY
“An IRB alternative and runtime
developer console” - http://pryrepl.org
Three major pieces
Pry-core
Pry-custom
Pry-ecosystem
WHAT IS PRY?
Commands are the primary way of
interacting with Pry
Higher order of precedence than ruby code
Expandable
Regex based
PRY-CORE
binding.pry
play
wtf
whereami
? (show-doc)
$ (show-source)
edit
COMMANDS
ls
cd
amend-line
Also !s/foo/bar
.<shell_command>
exit
_
_pry_
_ex_
_in_
_out_
_file_
_dir_
SPECIAL LOCALS
Result of last line
Local pry instance
Last exception
Array of all ruby input expressions
Array of all ruby output results
Last referenced file
Last referenced directory
Pry can be configured by a .pryrc file
~/.pryrc
./.pryrc
Project level pryrc files are especially useful
when paired with custom commands
PRY-CUSTOM
Almost everything in pry is configurable
Some configuration can even be
modified at runtime via _pry_.<config>
Of particular importance/interest:
Editor
Command prefix
Custom commands
BASIC CUSTOMIZATIONS
When using „edit‟ pry uses this setting
If editor is not set, Pry defaults to nano
You can set editor to a proc which
accepts 2 params: file and line
E.g. for sublime text:
EDITOR
COMMAND PREFIX
A problem and a solution
COMMAND PREFIX
A more permanent solution
Pry.config.command_prefix = „%‟
Now all pry commands must be prefixed
with „%‟
CUSTOM COMMANDS
Pry also allows you to define your own
custom commands
By block or class
Useful for often repeated code that
shouldn‟t be checked in
CUSTOM COMMANDS
Suppose you need to clean your
database from time to time
CUSTOM COMMANDS
Programmers are lazy (in a good way)
We like our code DRY and expressive
Where is that laziness when it comes to
the console?
Pry + custom commands = DRY,
expressive console
Any gem whose name fits the format pry-
<plugin> is a plugin
Beware: these gems are auto-loaded by
pry
--no-plugins option will skip auto-loading
.pryrc files get loaded before plugins
Plugin specific suppression:
Pry.plugins[“debugger”].disable!
PRY-ECOSYSTEM (PLUGINS)
Pry-debugger
Works only in MRI 1.8 – 1.9
Pry-byebug
A fork of pry-debugger
Works in MRI 2.0 – 2.1
Pry-nav
Pure ruby implementation
Works on all rubies
DEBUGGERS
Pry-stack-explorer
Gives pry commands: up, down, frame, and show-
stack
Pry-rescue (Pry-exception_explorer has been
deprecated)
Allows you to rescue from exceptions into a pry
session
Rescue for entire program, or only for specified
blocks
Plymouth
Jump into a pry session on test failure
WHEN THINGS BREAK
Uses regex magic to add some easy-access
syntax hackery
Accessing instance variables
foo.@instance_var
Calling private methods
foo.!private_meth()
Examining things in parent pry sessions
puts ../local_var
Just be careful with your regexes
PRY-SYNTAX-HACKS
Pry-doc
Adds core ruby class documentation for “?” and
“$” commands
Pry-vterm_aliases
Makes your ZSH and Bash aliases available to
pry‟s .<shell> command
Pry-theme
Color themes for pry‟s syntax highlighting
MISCELLANEOUS PLUGINS
Pry-rails
Turns rails console into a pry session
Teaches Pry new commands
show-[middleware|model|models|route]
recognize-path
Pry-plus
A pre-packaged group of useful plugins
INTEGRATION
Puts is ok. But don‟t hesitate to drop back and
use the big guns
REPL driven programming is incredibly powerful
Especially when working with new code/library
Explore code
Bundle open your favorite gem when it bugs out (or
just cd into it)
You never know what you‟ll find
CONCLUSION

More Related Content

What's hot

Getting Started with (Distributed) Version Control
Getting Started with (Distributed) Version ControlGetting Started with (Distributed) Version Control
Getting Started with (Distributed) Version Control
John Paulett
 
Ruby projects of interest for DevOps
Ruby projects of interest for DevOpsRuby projects of interest for DevOps
Ruby projects of interest for DevOps
Ricardo Sanchez
 
Os Leventhal
Os LeventhalOs Leventhal
Os Leventhal
oscon2007
 
20140419 oedo rubykaigi04
20140419 oedo rubykaigi0420140419 oedo rubykaigi04
20140419 oedo rubykaigi04
Hiroshi SHIBATA
 

What's hot (20)

20140918 ruby kaigi2014
20140918 ruby kaigi201420140918 ruby kaigi2014
20140918 ruby kaigi2014
 
Deconstruct 2017: All programmers MUST learn C and Assembly
Deconstruct 2017: All programmers MUST learn C and AssemblyDeconstruct 2017: All programmers MUST learn C and Assembly
Deconstruct 2017: All programmers MUST learn C and Assembly
 
Do it Yourself Testing
Do it Yourself TestingDo it Yourself Testing
Do it Yourself Testing
 
Getting Started with (Distributed) Version Control
Getting Started with (Distributed) Version ControlGetting Started with (Distributed) Version Control
Getting Started with (Distributed) Version Control
 
My talk on Piter Py 2016
My talk on Piter Py 2016My talk on Piter Py 2016
My talk on Piter Py 2016
 
Ruby projects of interest for DevOps
Ruby projects of interest for DevOpsRuby projects of interest for DevOps
Ruby projects of interest for DevOps
 
CRaSH the shell for the JVM
CRaSH the shell for the JVMCRaSH the shell for the JVM
CRaSH the shell for the JVM
 
Brief introduction to kselftest
Brief introduction to kselftestBrief introduction to kselftest
Brief introduction to kselftest
 
Composer Helpdesk
Composer HelpdeskComposer Helpdesk
Composer Helpdesk
 
How to develop the Standard Libraries of Ruby?
How to develop the Standard Libraries of Ruby?How to develop the Standard Libraries of Ruby?
How to develop the Standard Libraries of Ruby?
 
CPAN Training
CPAN TrainingCPAN Training
CPAN Training
 
CRaSH the shell for the Java Virtual Machine
CRaSH the shell for the Java Virtual MachineCRaSH the shell for the Java Virtual Machine
CRaSH the shell for the Java Virtual Machine
 
Slack Bots in Ruby
Slack Bots in RubySlack Bots in Ruby
Slack Bots in Ruby
 
Os Leventhal
Os LeventhalOs Leventhal
Os Leventhal
 
20140419 oedo rubykaigi04
20140419 oedo rubykaigi0420140419 oedo rubykaigi04
20140419 oedo rubykaigi04
 
How to write a well-behaved Python command line application
How to write a well-behaved Python command line applicationHow to write a well-behaved Python command line application
How to write a well-behaved Python command line application
 
20140925 rails pacific
20140925 rails pacific20140925 rails pacific
20140925 rails pacific
 
The Parenscript Common Lisp to JavaScript compiler
The Parenscript Common Lisp to JavaScript compilerThe Parenscript Common Lisp to JavaScript compiler
The Parenscript Common Lisp to JavaScript compiler
 
Plumbin Pipelines - A Gulp.js workshop
Plumbin Pipelines - A Gulp.js workshopPlumbin Pipelines - A Gulp.js workshop
Plumbin Pipelines - A Gulp.js workshop
 
The problem with Perl
The problem with PerlThe problem with Perl
The problem with Perl
 

Similar to Debugging Ruby (with Pry)

Using puppet
Using puppetUsing puppet
Using puppet
Alex Su
 

Similar to Debugging Ruby (with Pry) (20)

Exploring Code with Pry!
Exploring Code with Pry!Exploring Code with Pry!
Exploring Code with Pry!
 
A Fabric/Puppet Build/Deploy System
A Fabric/Puppet Build/Deploy SystemA Fabric/Puppet Build/Deploy System
A Fabric/Puppet Build/Deploy System
 
Daemons in PHP
Daemons in PHPDaemons in PHP
Daemons in PHP
 
REPL-driven development with pry
REPL-driven development with pry  REPL-driven development with pry
REPL-driven development with pry
 
2009 cluster user training
2009 cluster user training2009 cluster user training
2009 cluster user training
 
Rails 勉強会#3
Rails 勉強会#3Rails 勉強会#3
Rails 勉強会#3
 
Docker Introduction.pdf
Docker Introduction.pdfDocker Introduction.pdf
Docker Introduction.pdf
 
Learn flask in 90mins
Learn flask in 90minsLearn flask in 90mins
Learn flask in 90mins
 
Docker perl build
Docker perl buildDocker perl build
Docker perl build
 
Drush 5.0 (DrupalCamp LA 2012) - Chris Charlton
Drush 5.0 (DrupalCamp LA 2012) - Chris CharltonDrush 5.0 (DrupalCamp LA 2012) - Chris Charlton
Drush 5.0 (DrupalCamp LA 2012) - Chris Charlton
 
Using puppet
Using puppetUsing puppet
Using puppet
 
Developing IT infrastructures with Puppet
Developing IT infrastructures with PuppetDeveloping IT infrastructures with Puppet
Developing IT infrastructures with Puppet
 
Learn enough Docker to be dangerous
Learn enough Docker to be dangerousLearn enough Docker to be dangerous
Learn enough Docker to be dangerous
 
Intro django
Intro djangoIntro django
Intro django
 
Shared Object images in Docker: What you need is what you want.
Shared Object images in Docker: What you need is what you want.Shared Object images in Docker: What you need is what you want.
Shared Object images in Docker: What you need is what you want.
 
Using filesystem capabilities with rsync
Using filesystem capabilities with rsyncUsing filesystem capabilities with rsync
Using filesystem capabilities with rsync
 
Docker jako prostředí pro automatizaci testů
Docker jako prostředí pro automatizaci testůDocker jako prostředí pro automatizaci testů
Docker jako prostředí pro automatizaci testů
 
Cloud meets Fog & Puppet A Story of Version Controlled Infrastructure
Cloud meets Fog & Puppet A Story of Version Controlled InfrastructureCloud meets Fog & Puppet A Story of Version Controlled Infrastructure
Cloud meets Fog & Puppet A Story of Version Controlled Infrastructure
 
Docker Basics & Alfresco Content Services
Docker Basics & Alfresco Content ServicesDocker Basics & Alfresco Content Services
Docker Basics & Alfresco Content Services
 
Docker Security workshop slides
Docker Security workshop slidesDocker Security workshop slides
Docker Security workshop slides
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 

Debugging Ruby (with Pry)

  • 2. ABOUT ME Luke Bergen Twitter: @lbergen Reviewed.com
  • 3. Developers spend 60% of their time debugging puts doesn‟t always do the trick Enter pry and friends DEBUGGING IN RUBY
  • 4. “An IRB alternative and runtime developer console” - http://pryrepl.org Three major pieces Pry-core Pry-custom Pry-ecosystem WHAT IS PRY?
  • 5. Commands are the primary way of interacting with Pry Higher order of precedence than ruby code Expandable Regex based PRY-CORE
  • 7. _ _pry_ _ex_ _in_ _out_ _file_ _dir_ SPECIAL LOCALS Result of last line Local pry instance Last exception Array of all ruby input expressions Array of all ruby output results Last referenced file Last referenced directory
  • 8. Pry can be configured by a .pryrc file ~/.pryrc ./.pryrc Project level pryrc files are especially useful when paired with custom commands PRY-CUSTOM
  • 9. Almost everything in pry is configurable Some configuration can even be modified at runtime via _pry_.<config> Of particular importance/interest: Editor Command prefix Custom commands BASIC CUSTOMIZATIONS
  • 10. When using „edit‟ pry uses this setting If editor is not set, Pry defaults to nano You can set editor to a proc which accepts 2 params: file and line E.g. for sublime text: EDITOR
  • 11. COMMAND PREFIX A problem and a solution
  • 12. COMMAND PREFIX A more permanent solution Pry.config.command_prefix = „%‟ Now all pry commands must be prefixed with „%‟
  • 13. CUSTOM COMMANDS Pry also allows you to define your own custom commands By block or class Useful for often repeated code that shouldn‟t be checked in
  • 14. CUSTOM COMMANDS Suppose you need to clean your database from time to time
  • 15. CUSTOM COMMANDS Programmers are lazy (in a good way) We like our code DRY and expressive Where is that laziness when it comes to the console? Pry + custom commands = DRY, expressive console
  • 16. Any gem whose name fits the format pry- <plugin> is a plugin Beware: these gems are auto-loaded by pry --no-plugins option will skip auto-loading .pryrc files get loaded before plugins Plugin specific suppression: Pry.plugins[“debugger”].disable! PRY-ECOSYSTEM (PLUGINS)
  • 17. Pry-debugger Works only in MRI 1.8 – 1.9 Pry-byebug A fork of pry-debugger Works in MRI 2.0 – 2.1 Pry-nav Pure ruby implementation Works on all rubies DEBUGGERS
  • 18. Pry-stack-explorer Gives pry commands: up, down, frame, and show- stack Pry-rescue (Pry-exception_explorer has been deprecated) Allows you to rescue from exceptions into a pry session Rescue for entire program, or only for specified blocks Plymouth Jump into a pry session on test failure WHEN THINGS BREAK
  • 19. Uses regex magic to add some easy-access syntax hackery Accessing instance variables foo.@instance_var Calling private methods foo.!private_meth() Examining things in parent pry sessions puts ../local_var Just be careful with your regexes PRY-SYNTAX-HACKS
  • 20. Pry-doc Adds core ruby class documentation for “?” and “$” commands Pry-vterm_aliases Makes your ZSH and Bash aliases available to pry‟s .<shell> command Pry-theme Color themes for pry‟s syntax highlighting MISCELLANEOUS PLUGINS
  • 21. Pry-rails Turns rails console into a pry session Teaches Pry new commands show-[middleware|model|models|route] recognize-path Pry-plus A pre-packaged group of useful plugins INTEGRATION
  • 22. Puts is ok. But don‟t hesitate to drop back and use the big guns REPL driven programming is incredibly powerful Especially when working with new code/library Explore code Bundle open your favorite gem when it bugs out (or just cd into it) You never know what you‟ll find CONCLUSION

Editor's Notes

  1. Let’s be honest. The vast majority of the time, “puts the_thing_that_makes_no_sense” is completely sufficient to figure out what’s going wrong in your programI don’t believe that we spend 60% of our time debugging the kinds of problems that can be solved with a puts
  2. Also, these are useful
  3. Some plugins like the debuggers hook into the Pryinit process so using pryrc suppression won’t always save the day.
  4. Demonstration of bug running ./todo.rb items buggy_list