I'm a Ruby, Clojure, and now Node.js developer. When I was doing Clojure, I had a REPL driven development style.
Those of you that don't know what a REPL is, it's a read-eval-print-loop. You've probably used a REPL before and didn't even know it. The interactive ruby shell, otherwise known as IRB is the default REPL that ships with Ruby. It's also the same REPL you get when you run rails console.
The difference between developing in a Lisp like Clojure and a language like Ruby is how integrated the REPL is to how you develop. In Clojure, you get to play with code and mold it.
People that haven't done this style of development before really should experience it. It'll change the way you'll think about programming and your workflow.
When I started doing some Ruby and Rails projects again, I missed having my REPL. Developing with the rails console opened in my terminal and writing methods line by line didn't cut it.
Pry also aims to be more than an IRB replacement; it is an attempt to bring REPL driven programming to the Ruby language. It is currently not as powerful as tools like SLIME for lisp, but that is the general direction Pry is heading.
I first heard about Pry during a job interview at Braintree. I've since checked it out and have have integrated it into my workflow.
I'll start with a simple tutorial to just Pry and since I and many of you are likely to be web developers, how to integrate it with Rails.
The first thing you want to do is install pry by simply running gem install pry.
Pry has many different commands, which are similarly named to the command line utilities you're already using, like cd and ls.
You can open up a Ruby project you've been working on or open up your favorite editor and type this in:
Run the script and you'll see start, and then the program will pause and you can play with it while it's running. Basically, the program pauses exactly where you put the binding.pry line so you can do some debugging if necessary.
You can use the shell as if you were to use irb. Enter in 1 + 1 and you'll get 2 and so forth.
And to exit, simply type exit.
Now here's the cool part. If you go back to the file and edit it to add in functions, you can hot reload the code.
So, if I were to go and add the function add like so:
You can go back to your repl and type in add(1,2). You'll see that you'll have an undefined method error. That's because we haven't reloaded the file yet.
Type in reload-code in pry and then you'll see the file is reloaded and the method add shows up.
Now if you do add(1,2), you can see the return value, which is of course 3.
It gets better when you add in classes. Let's do that:
Reload the code again in pry.
Again, you can do the same things you can do with irb.
A really cool thing you can do is list all the methods of an object.
Since we're listing the methods of the class, we don't get to see the instance variables. If you were to instead do ls car, using the instance of car we created, you would also get its instance variables outputted.
For help and a list of all commands, type help.
Open up the Gemfile of one of your Rails projects and add this line:
gem 'pry-rails', :group => :development
Now your default rails console has been replaced with pry instead of irb.
If you type in help, you'll see a list of Rails specific commands:
These commands are significantly faster than running the rake task because the Rails environment is already loaded for you.
Now you can go into your controller and add in the binding.pry line to start pry and it'll pause the execution of the request at that line and open up the pry shell where you ran rails s. You can now play and debug your application without having to use puts everywhere.