Closure in Ruby

      No Comments on Closure in Ruby

What is Closure?

A closure is a function or a method or a block in case of ruby which has an environment/scope of its own and that environment/scope can have local variables, bindings which can be used at the place where that function is getting executed.

What is closure in ruby?

In ruby closure is nothing but a block or lambda which captures the bindings of the scope where its defined. That binding can have local variables defined inside the block or any variable that was available to the block at the time when it was defined.

Lets understand via an example

In the above code snippet we have a Printer class, which prints whatever is passed in the block. Now our block which is passed to the print method is a closure. And this closure has a variable in its binding which is being used in the print method of Printer class.

Can a closure modify the value of variable which is bound to it?

So the answer is yes, a closure can change the value of its variable which is bound to it. This is because when a block is passed, it contains the references of the variables bounded to it. hence if you modify the variable it will take effect everywhere.

What if the scope where block is getting executed has a variable with the same name which is present in a block, which value will it pick?

Value of inside the change method doesn’t get modified instead it modifies the variable which was available inside the closure/block. So this explains that, a block uses only those variables which were present when it was defined.

Leave a Reply

Your email address will not be published. Required fields are marked *