Ruby Bound & ERB
What is “binding” in Ruby? Up until now we have really only seen binding used in debugging code with the ‘pry’ gem and ‘binding.pry’. It allowed to to essentially freeze our interpreter in a certain state of the code, letting us check what the state our variables are in that given moment.
But is there more to binding? Well there must be because we have actually been calling the #pry method on a binding object this whole time!
Binding is a class whose instances (binding objects)…
“encapsulate the execution context at some particular place in the code and retain this context for future use” — Ruby-doc (Class: Binding)
What this means is a binding object captures and stores the local scope where it was called into itself. We can later pass these binding objects as an argument for the #eval method (evaluation) to peek into the the scope at the time of block execution.
In the above example, we have defined a method #foo to set a local variable, x, equal to 1. We then placed a binding object within the method. Upon calling the method (line 6), we stored the binding object into a local variable “binding”. At this point, even though we have called #foo, the variable “x” is undefined in the main scope. However, with the use of #eval, we can pass the variable and the binding object to determine what the value of “x” is inside the method scope. In a sense, the binding captured the moment after #foo was called and ‘x’ was defined, but before the method had ended.
Another short example shows how we are able to use binding objects in Classes. How is it that we are able to read an instance variable of a Class without an attr_accessor? Well the binding object once again has stored the scope within allowing us to evaluate inside that scope of the instance.
Get Robert Lin’s stories in your inbox
Join Medium for free to get updates from this writer.
To be fair, these are very simple examples and outside of debugging with ‘binding.pry’, the usage of a binding object seems obscure at best. How is binding significant to us? Well in week 3 of Flatiron School, we have just started learning about ERB (short for Embedded RuBy) and its usefulness in creating an HTML file with Ruby code embedded in it. Ruby on Rails will evaluate the Ruby code, allowing us to dynamically add content to the HTML file before it gets rendered. This process is referred to as ERB templating.
In an ERB templating process we pass our ‘.html.erb’ file to create a new ERB template object such that we may render the template in a scope where our code is run (with binding!) and store the result to be sent as a response. The binding passed with the #result method on an ERB template references the state in time (scope) when the code is run, producing a rendered ERB.
If we look back at our ‘.pry’ method, it makes sense why it would utilize a Binding object. When ‘binding.pry’ freezes a block execution it allows us access inside the scope of the block, the same scope that is captured by that very instance of Binding. The Binding Class in Ruby is truly a unique and useful tool worth exploring in the Ruby toolbox!







