RubyCademy
Published in

RubyCademy

Context Binding in Ruby

In this article we’re going to explore the following topics:

  • The Binding class
  • The TOPLEVEL_BINDING constant
  • ERB Templates

Before to start

Please allow me to introduce here the platform that helped me to learn a big part of my knowledge about Ruby. Indeed, Pluralsight is an amazing platform.

With 50+ courses that cover various topics on Ruby and Ruby on Rails, it’s the best way to take your knowledge to the next level!

Try it for free 👇😉

Thank you for your time!

The Binding class

An instance of the Binding class is able to encapsulate the execution context of the receiver object.

The execution context is the entire environment needed for a given piece of code to be executed. A code without execution context cannot be properly executed.

The execution context contains the variables, the methods and self for a given scope.

The Execution Context is a bit more complicated than this. Anyway, let’s keep it simple as it’s not the main topic of this article.

I’ll detail this key-concept in another article.

The Binding class is not publicly instantiable — as the new method is undefined during the Binding class initialization.

Otherwise, this class can be internally instantiated via the Kernel#binding method.

As the Kernel#binding method is private, we access this method through the Context#get_binding method.

So the real purpose of this class is:

Encapsulating an Execution Context and being able to access it from another Execution Context.

I know that this notion can be a bit tricky to understand.

Anyway, we’re going to detail 2 concrete examples in the next two sections.

NB: feel free to have a look to the private and protected: a matter of message article if you’re unfamiliar with the private keyword in Ruby.

The TOPLEVEL_BINDING constant

The main object is the top-level scope. Any object in Ruby is instantiated, at least, under this scope.

NB: feel free to have a look to the Ruby Object Model article if you’re unfamiliar with the main object in Ruby.

In order to access the Execution Context of the main object at any time, Ruby provides a global constant named as TOPLEVEL_BINDING

The Binding#receiver method returns the receiver of the Kernel#binding message. So, the calling Execution Context is retained — in our case, the main object.

Then we access the @a variable within an instance of the Addition class using the TOPLEVEL_BINDING global constant.

ERB templates

Let’s illustrate the use of the Binding concept by breaking down a Ruby on Rails concept that you’ve surely worked with: the ERB templates

In app/controllers/users_controller.rb

In app/views/users/show.html.erb

Here the show.html.erb template gets access to the @user instance variable from the UsersController class.

So, how it works behind the scene?

The ERB class provides a result method that takes an instance of the Binding class as parameter.

So let’s redo the previous example by explicitly instantiating the ERB class to make you understand what happens behind the scene

So, in this example, the instance of ERB can access the @user variable defined in the UsersController instance via the Binding instance passed as parameter of the ERB#result method.

Voilà!

Thank you for taking the time to read this post :-)

Feel free to 👏 and share this article if it has been useful for you. 🚀

Here is a link to my last article: The Enumerable module in Ruby: Part II.

--

--

--

E-Learning platform for Ruby and Ruby on Rails

Recommended from Medium

My Journey to Hybrid Cloud

CS373 Fall 2020: Christopher Chen

Flutter — IDE Shortcuts for Faster Development

Netflix Case-Study : AWS

Editing API Driver API Profiles

2. Why We Implemented A Micro Frontend

Lisk Dev Update: Lisk Core 1.4.1’s

Fresh Resources for Web Designers and Developers (May 2021)

brandbird

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tech - RubyCademy

Tech - RubyCademy

rubycademy.com

More from Medium

Create a Rails API with React Frontend using Hooks and TypeScript

When and Why use React with Ruby on Rails?

How to use .includes? in Rails 7

Ruby: Active Record Overview