Skip to content

Instantly share code, notes, and snippets.

@QuintinAdam
Last active December 24, 2015 01:29
Show Gist options
  • Save QuintinAdam/6723566 to your computer and use it in GitHub Desktop.
Save QuintinAdam/6723566 to your computer and use it in GitHub Desktop.
Useing show-doc for Pry

Show-doc for Pry

Browsing documentation in Pry is easy with the show-doc command. If you every want to look a methods documentation while working on your code, show-doc will save you time.

Basics of show-doc

Running the -h or --help option on the show-doc command will give you a basic over view of the different options and examples.

[1] pry(main)> show-doc -h

You can view the documentation of a class or method by simple running:

[1] pry(main)> show-doc MethodName

or by using the short-hand version of show-doc:

[1] pry(main)> ? MethodName

Examples

Finding documentation on the flatten method of an array.

[1] pry(main)> cd Array
[2] pry(Array):1> ? flatten
  
From: array.c (C Method):
Owner: Array
Visibility: public
Signature: flatten(*arg1)
Number of lines: 15

Returns a new array that is a one-dimensional flattening of self
(recursively).

That is, for every element that is an array, extract its elements into
the new array.
 
The optional level argument determines the level of recursion to
flatten.

s = [ 1, 2, 3 ]           #=> [1, 2, 3]
t = [ 4, 5, 6, [7, 8] ]   #=> [4, 5, 6, [7, 8]]
a = [ s, t, 9, 10 ]       #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
a.flatten                 #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a = [ 1, 2, [3, [4, 5] ] ]
a.flatten(1)              #=> [1, 2, 3, [4, 5]]

You can also get the same results without switching the directory.

[5] pry(main)> ? Array#flatten

If you forgot what parameters your defined method takes or even what your method dose. You could run the show-docs command on your method and it will return with any comments and information about your method.

[1] pry(main)> load 'encryptor.rb'
Enter your password:  *******
You are now logged in!
=> true
[2] pry(main)> ? Encryptor#encrypt
    
From: encryptor.rb @ line 38:
Owner: Encryptor
Visibility: public
Signature: encrypt(string=?, rotation=?)
Number of lines: 1
    
Takes the input and splits it into an array then passes it to the encrypt_letters method.

From that I can see what line the method is at, that it takes two arguments and a description of what the method dose.

Conclusion

By using show-doc you can find a lot of great information just by looking through the documentation of different methods. It also provides a quick way to find the information you need with out having to leave your terminal/iTerm window.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment