Connect With Us

What is `binding` object in Ruby?

By Shiva Bhusal on Jun, 03, 2016 in ruby, binding, context, ERB


Lets see an example before we explain the term binding

def binding1
  occupation = 'chor'

def binding2
  occupation = 'engineer'

erb = "Ram prasad is <%= occupation %>"
# => "Ram prasad is chor"
# => "Ram prasad is engineer"

Looking into the example, some of you might have gotten the idea that binding is nothing more than an object representing a closure where variables/objects are defined. Objects of class Binding encapsulate the execution context at some particular place in the code and retain this context for future use. You can also imagine this as a variable representing container where stuffs are defined.

For more info see this


class Demo
  def initialize(n)
    @secret = n
  def get_binding
    return binding()

k1 =
b1 = k1.get_binding
k2 =
b2 = k2.get_binding

eval("@secret", b1)   #=> 99
eval("@secret", b2)   #=> -3
eval("@secret")       #=> nil

Share This:


Add a new comment

Latest Posts

Design Sprint By Neha Suwal on Aug, 14, 2017

3 years of helping startups and entrepreneurs with technology By Neha Suwal on Aug, 04, 2017

Digital Marketing for Startups By Neha Suwal on Jun, 30, 2017

Jyaasa in Prestige Talks By Neha Suwal on Jun, 16, 2017

Helping digital era entrepreneurs: How Jyaasa helped create a tech startup success story in Sydney. By Kapil Raj Nakhwa on May, 29, 2017