The one thing all Pryers probably use is binding.pry .

What does it do? If the Ruby interpreter comes across that magical line, it’ll stop execution and start a REPL (read-evaluate-print-loop), allowing us to dynamically type Ruby code, which gets evaluated, and we can see the result! Amazing stuff.

But what is it? The magical binding.pry we Pryers constantly type?

Let’s stop looking at it as a magical keyword and look at some basic Ruby:

$ rails console

pry(main)> text = “sample string”

pry(main)> text.upcase

=> "SAMPLE STRING"

What did we do? We simply invoked an instance method over the String object text. It’s the same as binding.pry :

$ rails console

pry(main)> binding

=> #<Binding:0x007fee1297a308>

pry(main)> binding.class

=> Binding

We can see that binding is an actual object here, of the Binding class. But… what is it? Is it from Pry? Let’s start a rails console without Pry and check it out:

$ DISABLE_PRY_RAILS=1 rails console

irb(main)> binding

=> #<Binding:0x007fb29c85c4a0>

We still have it… so it’s not from Pry. Is it from Rails? Let’s boot IRB:

$ irb

irb(main)> binding

=> #<Binding:0x007fce81825f30>

So plain Ruby has the binding object? Hmm. I wonder what’s a binding then!

Enter Pry. One of its very powerful features is the command system, with which you can implement commands that fit your own personal workflows. But that’s not to be covered here.

Pry already has some useful commands for source code browsing implemented, and there’s a cool one we could really use right now to answer our question. Let’s try out show-doc :

pry(main)> show-doc binding From: proc.c (C Method):

Owner: Kernel

Visibility: private

Signature: binding()

Number of lines: 10 Returns a Binding object, describing the variable and

method bindings at the point of call. This object can be used when

calling eval to execute the evaluated command in this

environment. See also the description of class Binding. def get_binding(param)

return binding

end

b = get_binding("hello")

eval("param", b) #=> "hello"

We used one of Pry’s commands: show-doc . It shows us the documentation for any given method on the go!

So now we know: binding is actually an object that describes a variable and its state at the point of call.

So what is pry? Let’s do the same with pry… we reached the conclusion it was an instance method, right? Let’s try calling show-doc over the binding.pry itself and see if it works:

pry(main)> show-doc binding.pry From: (...)/gems/pry-0.10.4/lib/pry/core_extensions.rb @ line 23:

Owner: Object

Visibility: public

Signature: pry(object=?, hash=?)

Number of lines: 18 Start a Pry REPL on self. If `self` is a Binding then that will be used to evaluate expressions;

otherwise a new binding will be created.

(__deprecated__, use `object.pry`)

param [Hash] hash the options hash

example With a binding

binding.pry

example On any object

"dummy".pry

example With options

def my_method

binding.pry :quiet => true

end

my_method()

Pry.start param [Object] object the object or binding to pry(__deprecated__, use `object.pry`)param [Hash] hash the options hashexample With a bindingbinding.pryexample On any object"dummy".pryexample With optionsdef my_methodbinding.pry :quiet => trueendmy_method() @see Pry.start

It works: Pry’s parser understands that you’re interested in seeing the documentation for the method you’re calling, not the object.

So now we know pry is actually just a regular method that starts a Pry REPL over our binding object.

When we’re getting our binding.pry onto a Controller, Model, or some other Object, all we are doing is starting a Pry REPL over the binding of, for example, an ApplicationController or ActiveRecord::Base object, which would be the self described in the above pry documentation. Which means binding.pry is equivalent to pry self.send(:binding) (binding is a private method, so you have to send it.), in which we start a Pry REPL over the binding of the current self object.

So now we understand what binding.pry is, and learned about show-doc along the way!

I wonder if there’s any options we can pass show-doc ? We can call help show-doc to check this:

pry(main)> help show-doc Usage: show-doc [OPTIONS] [METH]

Aliases: ? Show the documentation for a method or class. Tries instance methods first and

then methods by default. show-doc hi_method # docs for hi_method

show-doc Pry # for Pry class

show-doc Pry -a # for all definitions of Pry class (all monkey patches) -s, --super Select the 'super' method. Can be repeated to traverse the ancestors

-l, --line-numbers Show line numbers

-b, --base-one Show line numbers but start numbering at 1 (useful for `amend-line` and `play` commands)

-a, --all Show all definitions and monkeypatches of the module/class

-h, --help Show this message.