CSC/ECE 517 Fall 2012/ch1 1w4 aj: Difference between revisions
No edit summary |
No edit summary |
||
Line 2: | Line 2: | ||
===Code Blocks=== | |||
Before understanding the concept of Closures, let us take a brief introduction on “Code Blocks” in Ruby. A code block is a chunk of code, Ruby statements and expressions written between curly braces { } or between “do…end”. | Before understanding the concept of Closures, let us take a brief introduction on “Code Blocks” in Ruby. A code block is a chunk of code, Ruby statements and expressions written between curly braces { } or between “do…end”. | ||
Line 28: | Line 26: | ||
</pre> | </pre> | ||
==Closures in Ruby== | |||
===What is a Closure?=== | |||
Now, a block can use local variables from the surrounding scope. Such blocks are called <b>Closures</b>. Let us look at a simple example: | Now, a block as shown before can use local variables from the surrounding scope. Such blocks are called <b>Closures</b>. Let us look at a simple example: | ||
<pre> | <pre> | ||
Line 45: | Line 44: | ||
In the above example, the value ‘3’ is assigned to the local variable <code>inc</code> of method <code>closurefunc</code> and value ‘1’ is assigned to inner variable <code>val</code>. | In the above example, the value ‘3’ is assigned to the local variable <code>inc</code> of method <code>closurefunc</code> and value ‘1’ is assigned to inner variable <code>val</code>. | ||
===How does it work?=== | |||
Let us look at a similar example, but this time, with strings. | |||
<pre> | |||
def concat() | |||
lambda { |greet| greet + param } | |||
end | |||
p1 = concat("John") | |||
p1.call("Hello") # => "HelloJohn" | |||
p2 = concat("Jim") | |||
p2.call("Good morning ") # => "Good morning Jim" | |||
</pre> | |||
The method <code>concat</code> returns a <code>Proc</code> object that references the method’s parameter, <code>param<code>. | |||
We need to use the call method of the <code>Proc</code> object to execute it. Even though the parameter <code>param</code> is out of scope when the block is called, the parameter is still accessible to the block. This is called a closure; where in the variables in the surrounding scope referenced in a block remain accessible for the life of that block and the life of any <code>Proc</code> object created from that block. |
Revision as of 20:56, 14 September 2012
Introduction
Code Blocks
Before understanding the concept of Closures, let us take a brief introduction on “Code Blocks” in Ruby. A code block is a chunk of code, Ruby statements and expressions written between curly braces { } or between “do…end”. For example:
{ puts "Hello World!" }
or
do 3.times(puts "Hello") object1.call end
Generally, as per Ruby standard, braces are used for single-line blocks and do...end for multiline blocks. Also, braces have a higher preference than do/end. A code block may appear only immediately after a method is invoked. If a method has parameters, then the block will look as follows:
random_method("John") { puts "How you doing? " }
Closures in Ruby
What is a Closure?
Now, a block as shown before can use local variables from the surrounding scope. Such blocks are called Closures. Let us look at a simple example:
def closurefunc() lambda {|val| val + inc } end p1 = closurefunc(3) p1.call(1) # => 4 p2 = closurefunc(8) p2.call(5) # => 13
In the above example, the value ‘3’ is assigned to the local variable inc
of method closurefunc
and value ‘1’ is assigned to inner variable val
.
How does it work?
Let us look at a similar example, but this time, with strings.
def concat() lambda { |greet| greet + param } end p1 = concat("John") p1.call("Hello") # => "HelloJohn" p2 = concat("Jim") p2.call("Good morning ") # => "Good morning Jim"
The method concat
returns a Proc
object that references the method’s parameter, param
.
We need to use the call method of the
Proc
object to execute it. Even though the parameter param
is out of scope when the block is called, the parameter is still accessible to the block. This is called a closure; where in the variables in the surrounding scope referenced in a block remain accessible for the life of that block and the life of any Proc
object created from that block.