wmock
wmock

Reputation: 5492

Variable Binding

I am not sure if I understand what variable binding means. This might be a programming concept that isn't specific to Ruby. It almost seems like its a basic concept that some books expect you to already know what that means.

Here's my understanding so far: Whenever a Ruby object is instantiated, what this actually means is that Ruby allocates a space in memory to designate this object. If you assign an object to a variable, you're not actually storing the object in the variable but instead you're storing the reference to that object in the variable. I think binding refers to this concept where the variable is "bound" to the object associated with it, but I'm not entirely sure if this represents the meaning of binding and bound.

Can someone help explain this to me, preferably through the use of a simple example if possible?

Upvotes: 2

Views: 1113

Answers (2)

Zach Kemp
Zach Kemp

Reputation: 11904

A good way to think about it is that bindings are objects that encapsulate context. You can reveal binding objects using the binding keyword:

a = 10 # a is defined in the 'main' context
def get_binding  # so is the 'get_binding' method
  b = 20
  binding
end

n = get_binding #=> #<Binding:0x00...>

Now since get_binding is defined within the main context, the returned binding object includes anything within the method's local context as well as the main context. The variable b is not available in main, while a is available in get_binding.

You can demonstrate this by interacting with the binding object using eval:

a #=> 10
eval('a', n) #=> undefined local variable or method 'a' for main:Object

b #=> undefined local variable or method `b' for main:Object
eval('b', n)  #=> 20  # but it is defined within the context the n binding was created.

Just to clarify - this example just reveals what happens behind the scenes. You will very rarely, if ever, need to deal directly with binding objects.

Ruby modules, classes, and methods reveal their bindings to objects lower down the hierarchy than them, but not the other way around, unless they are explicitly revealed through public instance methods, etc. This is a bit of an oversimplification, but it's really not necessary to get too deep into this if you're new to programming.

Upvotes: 1

sawa
sawa

Reputation: 168081

The concept you are mentioning in the second paragraph is about pointers, and is not directly related to binding.

As you noticed, binding is not specific to Ruby. It is a term widely used in formal handling of languages including programming languages as well as natural languages. There are expressions that have their own fixed meaning, which in programming languages are called constants, and in natural languages proper nouns (or, casual Americans also call it names). On the opposite are expressions that do not refer to anything by themselves but need to be assigned their value in some way. These are called variables in programming languages, and pronouns in natural languages.

In a context where a variable has not been assigned a value, the variable is said to be free. Otherwise, the variable is bound. Variable x in the following expression is free.

x**2

There are several ways a variable can be bound. One way is assignment. In the following, x has been assigned a value 3 in the first line, so it is bound within the scope.

x = 3
x**2

Another way is to quantify over it. In the following, the x within the block is bound by the x outside of it. Whatever value the outer x takes will be the value of the inner x.

->x{x**2}

On the other hand, the outer x in the above expression is not bound. By calling the proc against some value as below, it becomes bound.

->x{x**2}.call(3)

Upvotes: 2

Related Questions