CSC/ECE 517 Fall 2011/ch4 4b ds

From Expertiza_Wiki
Revision as of 12:31, 20 October 2011 by Ddilipd (talk | contribs)
Jump to navigation Jump to search

Wiki textbook chapter on OOLS Lecture 5. Covers the basic of Closures, blocks, currying and OOP in Ruby.

Introduction

Closures

A closure is a block of code that “closes over”. It can access the lexical environment of its definition. A closure may be defined in one scope and get called outside this scope. Thus a closure retains the values of all the variables that were in scope when the closure was defined. Closures help in making the code short such that one can do more with less code.

Closure in Ruby

Ruby supports closures through Blocks and Procs.

Blocks

A block is a set of Ruby statements either between braces or a do/end pair, and appear only immediately after a method invocation. Ruby uses the following standard - braces for single-line blocks and do/end for multiline blocks.