
Four Key Reasons to Learn Markdown
Back-End Leveling UpWriting documentation is fun—really, really fun. I know some engineers may disagree with me, but as a technical writer, creating quality documentation that will...
Ruby ships with an Interactive Ruby Shell (IRB) that every Ruby developer has played around with at some point. It’s a great place to experiment with unfamiliar methods and running bits of code, but if you really want to dig into an object or do hardcore debugging, it can leave a lot to be desired.
Pry is a great IRB alternative that has a number of features that make it one of my must-have tools.
Pry has five features that make it great:
You may often find yourself wondering about the difference between methods like #reject
and #reject!
, but you don’t feel that it warrants a full Google search. Pry provides a show-doc
method that makes this painless:
pry(main)> show-doc Array#sort! | |
From: array.c (C Method): | |
Number of lines: 9 | |
Owner: Array | |
Visibility: public | |
Signature: sort!() | |
Sorts self. Comparisons for | |
the sort will be done using the <=> operator or using | |
an optional code block. The block implements a comparison between | |
a and b, returning -1, 0, or +1. See also | |
Enumerable#sort_by. | |
a = [ "d", "a", "e", "c", "b" ] | |
a.sort #=> ["a", "b", "c", "d", "e"] | |
a.sort {|x,y| y <=> x } #=> ["e", "d", "c", "b", "a"] |
If you’re working with your own code and want to take a quick look at the source, there’s also show-method
:
pry(main)> def foo | |
pry(main)* puts "Welcome to the foo!!" | |
pry(main)* end | |
=> nil | |
pry(main)> show-method foo | |
From: (pry) @ line 3: | |
Number of lines: 3 | |
Owner: Object | |
Visibility: private | |
def foo | |
puts "Welcome to the foo!!" | |
end |
Pry’s hist --grep
makes it easy to search through your Pry history:
pry(main)> hist --grep foo | |
2: def foo | |
3: puts "Welcome to the foo!!" | |
5: show-method foo |
Rather than switch between Pry and your editor, you can use edit
to open up a file for editing:
pry(main)> edit dog.rb | |
# opens up vim where I add: | |
# | |
# class Dog | |
# def bark | |
# puts "Woof!" | |
# end | |
# end | |
pry(main)> my_dog = Dog.new | |
=> #<Dog:0x007feb4b821f20> | |
pry(main)> my_dog.bark | |
Woof! | |
=> nil | |
pry(main)> edit dog.rb | |
# opens up vim and I change it to: | |
# | |
# class Dog | |
# def initialize(bark) | |
# @bark = bark | |
# end | |
# | |
# def bark | |
# puts @bark | |
# end | |
# end | |
[8] pry(main)> his_dog = Dog.new("Rufff!") | |
=> #<Dog:0x007feb4b21a8c0 @bark="Rufff!"> | |
[9] pry(main)> his_dog.bark | |
Rufff! | |
=> nil | |
pry(main)> .ls | ack dog | |
dog.rb |
The only “gotcha” is that you need to make sure you specify your editor in a .pryrc
file:
Pry.config.editor = 'vim' |
Additionally, the -t
flag opens up a temp file, giving you a full editor to define a method, class or other ruby object, if you find doing it line-by-line in Pry to be too tedious.
You can run shell commands by prepending the command with a ‘.’. This is great when you’re working on a gem and want to edit a file without leaving Pry:
pry(main)> .cd ~/code/rspec | |
pry(main)> .ls | |
Gemfile License.txt README.md Rakefile lib rspec.gemspec | |
pry(main)> .pwd | |
/Users/greggory/code/rspec |
Any command with a ‘.’ in front will be forwarded to the shell, so you could even commit your code from Pry.
You can navigate around Ruby objects as if there were folders by using cd
and ls
:
pry(main)> my_array = [1, 2, 3, 4, 5] | |
=> [1, 2, 3, 4, 5] | |
pry(main)> ls | |
self.methods: include private public to_s | |
locals: _ _dir_ _ex_ _file_ _in_ _out_ _pry_ my_array | |
pry(main)> cd my_array | |
pry(#<Array>):1> ls | |
Enumerable#methods: all? any? chunk collect_concat detect each_cons each_entry each_slice each_with_index each_with_object | |
entries find find_all flat_map grep group_by inject max max_by member? min min_by minmax minmax_by none? one? | |
partition reduce slice_before sort_by | |
Array#methods: & * + - << <=> == [] []= assoc at clear collect collect! combination compact compact! concat count | |
cycle delete delete_at delete_if drop drop_while each each_index empty? eql? fetch fill find_index first flatten | |
flatten! frozen? hash include? index insert inspect join keep_if last length map map! pack permutation place pop | |
pretty_print pretty_print_cycle product push rassoc reject reject! repeated_combination repeated_permutation replace | |
reverse reverse! reverse_each rindex rotate rotate! sample select select! shelljoin shift shuffle shuffle! size slice | |
slice! sort sort! sort_by! take take_while to_a to_ary to_s transpose uniq uniq! unshift values_at zip | | |
self.methods: __pry__ | |
locals: _ _dir_ _ex_ _file_ _in_ _out_ _pry_ | |
pry(#<Array>):1> cd .. | |
pry(main)> |
ls
is useful for when you forget the name of that variable you defined earlier, and cd
can be useful for diving into objects.
This feature is available in IRb as well as Pry, but it’s something that I use very often:
pry(main)> [1, 2, 3, 4] | |
=> [1, 2, 3, 4] | |
pry(main)> a = _ | |
=> [1, 2, 3, 4] | |
pry(main)> a | |
=> [1, 2, 3, 4] |
These are just the highlights of Pry and some of the commands I use. You can find more information on the commands on the Pry wiki and by using the help
command in Pry. If you’re looking for more help with debugging, make sure you check out Aubrey’s post on pry-remote.
These are my favorite Pry features—what are yours?
Writing documentation is fun—really, really fun. I know some engineers may disagree with me, but as a technical writer, creating quality documentation that will...
Humanity has come a long way in its technological journey. We have reached the cusp of an age in which the concepts we have...
Go 1.18 has finally landed, and with it comes its own flavor of generics. In a previous post, we went over the accepted proposal and dove...