CSC/ECE 517 Fall 2011/ch4 4b ds: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
Wiki textbook chapter on OOLS Lecture 5. Covers the basic of Closures, blocks, currying and OOP in Ruby.
Wiki textbook chapter on OOLS Lecture 5. Covers the basic of Closures, blocks, currying and OOP in Ruby.
__TOC__
==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.

Revision as of 12:31, 20 October 2011

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.