CSC/ECE 517 Fall 2014/ch1a 26 gn: Difference between revisions
No edit summary |
No edit summary |
||
Line 19: | Line 19: | ||
Pry is written from scratch to provide a number of advanced features, some of these include : Substantiated claim.<ref>http://pryrepl.org</ref> | Pry is written from scratch to provide a number of advanced features, some of these include : Substantiated claim.<ref>http://pryrepl.org</ref> | ||
# Source code browsing (including core C source with the pry-doc gem) | # Source code browsing (including core C source with the pry-doc gem) | ||
Line 93: | Line 92: | ||
Whenever pry opens because of an exception you have two choices: | Whenever pry opens because of an exception you have two choices: | ||
# Hit <ctrl-d> to let the exception bubble on its way | # Hit <ctrl-d> to let the exception bubble on its way | ||
# Do some debugging. | |||
If you choose the latter path, then you can use the full power of pry to fix the problem; and then try-again to verify the fix worked. | |||
=References= | =References= |
Revision as of 01:39, 20 September 2014
Debugging in Rails using Pry
Ruby on Rails is a Web application framework for Ruby. It was first released to the public in July 2004. Within months, it was a widely used development environment. Many multinational corporations are using it to create Web applications. It is the standard Web-development framework for Ruby. Models are objects that represent the components of an application that perform information processing in the problem domain. Views are objects that display some aspect of the model. They are the output mechanism for the models. Controllers are objects that control how user actions are interpreted. They are the input mechanism for the views and models.
Since a Rails application goes beyond the model view controller, like Mailers, Routes (REST, HTTP-Verbs, Constraints), Environments and Initializers, Caching (Redis, Memcached), Assets (CSS, SASS,, JavaScript, CoffeeScript, Pipelining), Bundler and dependency management, Tests (RSpec, Capybara), gems, plugins and engines used in the application, one needs to know a lot of stuff to master debugging their own application. Debugging is one of the things a developer has to do most and there exists debuggers for Rails that are suitable for browsers and lack the ability to allow moving around in the call-stack and inspecting objects at runtime.
Pry in literal sense means to look or inquire closely, curiously, or impertinently. Pry is a powerful alternative to the standard IRB shell for Ruby. It not only for debugging but also (and mainly) for inspecting objects. It features syntax highlighting, a flexible plugin architecture, runtime invocation and source and documentation browsing. It enables the user to browse source code, inspect objects, eliminates all the iteration- and startup time when running red/green cycles and is especially useful when you don’t know exactly how something should function in the first place.
Pry: super-fast, painless, debugging for the (ruby) masses.
Pry aims to be more than an IRB replacement; it is an attempt to bring REPL driven programming to the Ruby language. Pry is also fairly flexible and allows significant user customization making it a good choice for implementing custom shells. The pry console gives you access to the method that raised the exception, you can use it to inspect the values of variables (no more print statements!), the source code of methods (no more flapping around with a text editor!), and even move up and down the call stack (like a real debugger!).
Because the shell opens as though the exception were just about to be raised, you don't even need to re-run your program from the beginning in order to start debugging. It's optimized for the "exceptions-happen" workflow that you need when developing code.
Features
Pry is written from scratch to provide a number of advanced features, some of these include : Substantiated claim.<ref>http://pryrepl.org</ref>
- Source code browsing (including core C source with the pry-doc gem)
- Navigation around state (cd, ls and friends)
- Rubinius core source browsing
- Documentation browsing
- Live help system
- Open methods in editors (edit-method Class#method)
- Syntax highlighting
- Command shell integration (start editors, run git, and rake from within Pry)
- Gist integration
- Runtime invocation (use Pry as a developer console or debugger)
- Exotic object support (BasicObject instances, IClasses, ...)
- A powerful and flexible command system
- Ability to view and replay history
- Many convenience commands inspired by IPython, Smalltalk and other advanced REPLs
Installation
The steps you have to do are simple:
$ gem install pry $ pry
You could install pry on Rails by adding the pry-rails gem to your gem file:
gem 'pry-rails', :group => :development
Once you have got that settled, change back into the sample_app directory and install all the necessary dependencies:
bundle install
Setting up the database:
rake db:schema:load
Note that some of you might need to prefix the above command with bundle exec.
One final thing before we proceed to the actual stuff: Create .pryrc in your home directory and fill it in with the editor of your choice:
% touch ~/.pryrc Pry.config.editor = 'vim'
To launch pry through the terminal you simply write:
rails c
and you’re greeted with a pry console:
% rails c Loading development environment (Rails 3.2.16) Frame number: 0/3 [1] sample_app »
Usage and Example
After you install Pry using the above mentioned install command, you run your program using rescue foo.rb instead of ruby foo.rb. If not, just wrap chunks of your program in Pry::rescue{ }. Any exceptions that are unhandled within that block will be rescued by pry on your behalf.
Whenever pry opens because of an exception you have two choices:
- Hit <ctrl-d> to let the exception bubble on its way
- Do some debugging.
If you choose the latter path, then you can use the full power of pry to fix the problem; and then try-again to verify the fix worked.
References
http://www.sitepoint.com/pry-friends-rails
http://cirw.in/blog/pry-to-the-rescue
http://www.jackkinsella.ie/2014/06/06/debugging-rails-with-pry-debugger.html