## Setup Replace IRB with Pry (in your Gemfile) and Byebug with `pry-byebug`. ```ruby gem 'pry-rails', group: [:development, :test] gem 'pry-byebug', group: [:development, :test] ``` ## Using PRY Put `pry` somewhere in your code to start pry session ```ruby def my_method pry ... end ``` ### Common commands - Show method source ```ruby pry> $ object.method # or class instance method pry> $ Class#method # or class static method pry> $ Class.method ``` - Show method docs ```ruby pry> ? object.method # or class instance method pry> ? Class#method # or class static method pry> ? Class.method ``` - Show object methods and instance variables ```ruby pry> ls object # or only public methods pry> ls -m object # or grep by name pry> ls -G has_ object # or instance variables pry> ls -i object ``` - Move between objects ```ruby pry> cd user pry(User):1> self => User # switch back pry(User):1> cd .. # or switch to the top pry(User):1> cd / ``` - Stop PRY session ```ruby # if you're in the top scope pry> exit # otherwise pry> exit-all # or pry> !!! ``` ### Rails specific commands - Show routes ```ruby pry> show-routes ``` - Show models info ```ruby pry> show-models pry> show-model User # or search model by attributes pry> show-models -G password ``` - Show controller routes ```ruby pry> find-routes UsersController ``` ## Debugging with `pry-byebug` - Start debug session ```ruby # somewhere in your code binding.pry ``` - Navigation ```ruby # step into the next line/method pry> step # or step multiple times pry> step 3 # step over to the next line/method pry> next # or pry> next 2 # exit current stack frame pry> finish # move the stack frame up/down pry> up pry> down pry> up 3 pry> down 4 # continue to the next breakpoint pry> continue # or pry> exit ``` - Breakpoints ```ruby # add breakpoint by the line number pry> break /path/to/project/app/models/user.rb:22 # or by class and method name pry> break User#admin? # disable all breakpoints pry> break --disable-all # disable by number pry> break -d 1 # enable by number pry> break -e 1 # breakpoint with condition pry> break User#my_method --condition 4 id == 2 # breaks only if id equals 2 ``` ## Capybara helpers - Save page as html ```ruby # Change output dir Capybara.save_and_open_page_path = "./tmp/capybara" # Add to your code to save current page state save_page # or save and open saved page in your browser save_and_open_page ``` [How to open saved page when using VM](http://spin.atomicobject.com/2014/06/15/testing-vagrant-web-apps/). - Save page as image ```ruby save_screenshot('path/to/my.png') ``` - Pause test server and login to test session ```ruby # Setup Capybara server (somewhere in acceptance_helper) Capybara.server_port = 3000 # or everything you want) Capybara.server_host = "0.0.0.0" # to access server running within VM # in your test code pry # simply start PRY session # and then open browser and login with current user credentials ``` ## Debugging js views ```js # view.erb.js debugger; $('.my-link').hide() ... ```