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?
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
xin the following expression is free.There are several ways a variable can be bound. One way is assignment. In the following,
xhas been assigned a value3in the first line, so it is bound within the scope.Another way is to quantify over it. In the following, the
xwithin the block is bound by thexoutside of it. Whatever value the outerxtakes will be the value of the innerx.On the other hand, the outer
xin the above expression is not bound. By calling the proc against some value as below, it becomes bound.