<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mrkamat</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mrkamat"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Mrkamat"/>
	<updated>2026-09-12T15:59:21Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=71123</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w47 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=71123"/>
		<updated>2012-11-20T03:43:22Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Directory of sites */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Adapter Pattern, a directory of sites'''=&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Adapter Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Sometimes, objects just don't fit together as they should. A class may have changed, or an object turns out to be just too difficult to work with. At such times, adapter design pattern comes to the rescue. Adapter pattern lets you adapt what an object or class has to offer so that another object or class can make use of it. It also helps when classes can't work together because of incompatible interfaces.&lt;br /&gt;
&lt;br /&gt;
Since we are huge fans of visual approach, lets understand the adapter pattern with the help of a diagram. Hold on! Before going in to the UML diagram ( which is kind of less attractive, right? May be .. ) lets look at the figure below which is more appealing.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternForWiki.jpg|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;The above example helps readers quickly get across the &amp;lt;b&amp;gt;intent&amp;lt;/b&amp;gt; of the pattern than the UML diagram shown below. Looking at the UML diagram now, can help the readers to learn about the precise relationships between the classes in adapter pattern and it makes a lot more sense!&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternUMLForWiki.jpg|thumb|500px|center|UML diagram for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, this is all what we had to talk about Adapter Pattern. Lets look at other websites and see what they say about Adapter pattern, (probably, the main purpose of the wiki page).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Directory of sites =&lt;br /&gt;
&lt;br /&gt;
=== [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Pattern on wikipedia] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Lists various structures of Adapter Pattern viz., Object Adapter Pattern, Class Adapter Pattern, Runtime Adapter pattern and implementation of Adapter pattern. It provides a few examples for the above structures.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).&amp;lt;br&amp;gt; The various Adapter pattern structures are as follows&lt;br /&gt;
* &amp;lt;b&amp;gt;Object Adapter pattern:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Class Adapter pattern:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;A further form of runtime Adapter pattern:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides a good example for runtime Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Provides a basic skeleton of how to implement the adapter pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This wikipedia page works if readers want a quick review of the adapter pattern. Also it provides good references to read more about the Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; It doesn't helps to understand the Adapter pattern in depth. The examples provided are not easy to comprehend ( unless you are an experienced developer and not a beginner! ). Its not a good page to refer for the beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://msdn.microsoft.com/en-us/library/orm-9780596527730-01-04.aspx Adpater Pattern on MSDN Microsoft Website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Role of adapter pattern, Illustrations or examples where adapter pattern is used ,Design and Implementation of adapter pattern, few more kinds of adapters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter pattern is a versatile pattern that joins together types that were not designed to work with each other. It is one of those patterns that comes in useful when dealing with legacy code-i.e., code that was written a while ago and to which one might not have access.&lt;br /&gt;
* &amp;lt;b&amp;gt;Role:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern enables a system to use classes whose interfaces don't quite match its requirements. It is especially useful for off-the-shelf code, for toolkits, and for libraries&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Illustration:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Adapter pattern illustration-migration of Mac OS X from a 1998 PowerPC-based iMac to a 2007 Intel-based iMac. This section shows how the adapter pattern has been used to enable Mac OS migration.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Design:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern's important contribution is that it promotes programming to interfaces. The Client works to a domain-specific standard, which is specified in the ITarget interface. An Adaptee class provides the required functionality, but with a different interface. The Adapter implements the ITarget interface and routes calls from the Client through to the Adaptee, making whatever changes to parameters and return types are necessary to meet the requirements. A Target class that implements the ITarget interface directly could exist, but this is not a necessary part of the pattern. In any case, the Client is aware only of the ITarget interface, and it relies on that for its correct operation.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides an example in C# and how the Adapter, Adaptee, Target and Client all communicate with each other.&lt;br /&gt;
* &amp;lt;b&amp;gt;Two way adapters and Pluggable adapters:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describes the other kinds of adapter with appropriate examples.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This page gives an in-depth explanation of the adapter pattern. It shows how the adapter pattern is implemented in C#. The real world examples makes it easy to grasp the topic.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; The page might be too long for a quick review. Though it is really good for a beginner, an Intermediate or an expert person in design pattern may not actually want to wade through the entire concept in such a depth. ( unless the person doesn't have much work to do...&amp;quot;eyebrows raised&amp;quot; ).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.oodesign.com/adapter-pattern.html Adpater Pattern on OODesign.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Motivation for using Adapter pattern, intent behind using the adapter pattern, a UML diagram to explain the adapter pattern ( like we did above, yeah! ) to start with. It also covers the application and implementation of Adapter patterns. Moreover, it provides some really nice examples and specific problems where adapter pattern becomes a good fit to use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;The adapter pattern is adapting between classes and objects. Like any adapter in the real world it is used to be an interface, a bridge between two objects. In real world we have adapters for power supplies, adapters for camera memory cards, and so on.&amp;lt;br&amp;gt;&lt;br /&gt;
What about software development? It's the same. Can you imagine an situation when you have some class expecting some type of object and you have an object offering the same features, but exposing a different interface? Of course, you want to use both of them so you don't to implement again one of them, and you don't want to change existing classes, so why not create an adapter...&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Intent&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Convert the interface of a class into another interface clients expect&lt;br /&gt;
# Adapter lets classes work together, that could not otherwise because of incompatible interfaces&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Explains the implementation of the Adapter pattern with the help of a UML diagram.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Applicability and Examples&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Adapters are encountered everywhere. From real world adapters to software adapters.&lt;br /&gt;
# Non Software Examples of Adapter Patterns: Power Supply Adapters, card readers and adapters, ...&lt;br /&gt;
# Software Examples of Adapter Patterns: Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code. &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Specific problem and Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Objects Adapters - Based on Delegation&amp;lt;br&amp;gt; It uses composition, the Adaptee delegates the calls to Adaptee (opossed to class adapters which extends the Adaptee).&lt;br /&gt;
# Class Adapters - Based on (Multiple) Inheritance&amp;lt;br&amp;gt; Class adapters can be implemented in languages supporting multiple inheritance(Java, C# or PHP does not support multiple inheritance). Thus, such adapters can not be easy implemented in Java, C# or VB.NET. Class adapter uses inheritance instead of composition.&lt;br /&gt;
# Adapter Pattern and Strategy Pattern&amp;lt;br&amp;gt; Cases when Adapter Pattern can be a better option instead of Strategy pattern ( ok, now that sounds something interesting, doesn't it?)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; Helps to learn Adapter pattern in detail. Provides good examples to understand how the pattern works and also give suitable cases where adapter design pattern is most applicable. Good for all readers ( beginners, intermediate and experts )&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; Doesn't provides enough figures to understand the concept. For readers who endorse figures, not a good page to look out for.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.codeproject.com/Articles/42915/Adapter-Design-Pattern Adpater Pattern on Codeproject.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Background, Introduction to adapter pattern and its structure, class diagram and implementation of adapter pattern for a scenario.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;This page uses a running example in the background to discuss the design pattern and then further provides a design pattern implementation for that particular example. Moreover, it explains the importance of Adapter Design in system integration when other components have to be adapted by the existing system.&lt;br /&gt;
* &amp;lt;b&amp;gt;Background:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The webpage continues using the running example to describe the earlier design patterns and further gives a scenario to introduce the adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Design and implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
It gives a class diagram describing the adapter pattern for the example in the background section. Also, this section covers the explanation and implementation of all the classes and interfaces mentioned in the below class diagram:&lt;br /&gt;
[[File:AdapterClassOnCodeProject.JPG|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This website is useful for intermediate users of design patterns. Some preexisting knowledge of adapter pattern prior to reading this page can help to further understand the topic. The example provided gives a clear view of the design pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; A beginner might find not find this page a good base line to start. The code on the page might be difficult for a novice programmer to understand.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternately, beginners can also refer the webpage on [http://www.codeproject.com/Articles/186003/Adapter-Design-Pattern Adapter Design Pattern] further provided by Codeproject.com &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://csc.columbusstate.edu/woolbright/java/adapter.html Adpater Pattern on Columbus State University website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Introduction to Adapter pattern, Object-Oriented Adapters, an example which implements the Adapter pattern and real world adapters.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Intrduction:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; The adapter pattern changes the look of an interface. It takes an interface and adapts it to one the client is expecting.&lt;br /&gt;
   The Adapter Pattern converts the interface of a class into another interface the client expects. &lt;br /&gt;
   Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Object-Oriented Adapters:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Assume your existing system expects interface A.&lt;br /&gt;
# You need the system to work with a vendor class that has interface C.&lt;br /&gt;
# The solution is an adapter class with interface A for the existing system, and interface C for working with the Vendor class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;The Duck Interface Example:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; The website provides a good example of the Adapter Design Pattern. The example is as follows: &amp;lt;br&amp;gt; The code shown below provides the Duck Interface:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public interface Duck {&lt;br /&gt;
        public void quack();&lt;br /&gt;
        public void fly();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:It is implemented by the MallardDuck class as shown below:&lt;br /&gt;
&lt;br /&gt;
    public class MallardDuck implements Duck {&lt;br /&gt;
        public void quack() {&lt;br /&gt;
            System.out.println(&amp;quot;Quack&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
         &lt;br /&gt;
        public void fly() {&lt;br /&gt;
            System.out.println(&amp;quot;I'm flying&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:There is another interface called the Turkey Interface:&lt;br /&gt;
&lt;br /&gt;
    public interface Turkey {  //Turkeys don't quack&lt;br /&gt;
        public void gobble();&lt;br /&gt;
        public void fly();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:The turkey interface is implemented by the wildTurkey class as shown below:&lt;br /&gt;
&lt;br /&gt;
    public class WildTurkey implements Turkey {&lt;br /&gt;
        public void gobble() {&lt;br /&gt;
            System.out.println(&amp;quot;Gobble gobble&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
         &lt;br /&gt;
        public void fly() {&lt;br /&gt;
            System.out.println(&amp;quot;I'm flying a short distance&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:And finally comes our &amp;lt;i&amp;gt;showstopper&amp;lt;/i&amp;gt;, the Turkey Adapter code as shown below: &lt;br /&gt;
&lt;br /&gt;
    public class TurkeyAdapter implements Duck {&lt;br /&gt;
        Turkey turkey;&lt;br /&gt;
         &lt;br /&gt;
        public TurkeyAdapter(Turkey turkey) {&lt;br /&gt;
            this.turkey = turkey;&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        public void quack() {&lt;br /&gt;
            turkey.gobble();&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        public void fly() {&lt;br /&gt;
            for(int i=0; i &amp;lt; 5; i++) {&lt;br /&gt;
                turkey.fly();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:The website also provides a good tester to test the Adapter code. Please refer to the link to see the test driving code. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Real world Adapters:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Following are the examples of real world adapters:&lt;br /&gt;
# The old Java Enumeration class had a couple of methods:&lt;br /&gt;
## hasMoreElements()&lt;br /&gt;
## nextElement()&lt;br /&gt;
# The new Java Iterator class has three methods:&lt;br /&gt;
## hasNext()&lt;br /&gt;
## next()&lt;br /&gt;
## remove()&lt;br /&gt;
&lt;br /&gt;
This section provides an &amp;lt;i&amp;gt;EnumerationIterator&amp;lt;/i&amp;gt; adapter class that implements the Iterator. Please refer to the link to see the &amp;lt;i&amp;gt;EnumerationIterator&amp;lt;/i&amp;gt; Adapter class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# A very good example of Adapter Pattern using the Duck Interface, Turkey Interface and the TurkeyAdapter pattern. ( Gosh! Too much of a Turkey used, reminds of Thanksgiving! ).&lt;br /&gt;
# Also, the section on real world adapters compliments further understanding of Adapter pattern implementation. Good for Intermediate and Expert users of Design patterns&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Does not contains enough content to understand Adapter pattern from scratch.&lt;br /&gt;
# Also, not suitable for beginners. Though the code provided is lucid and self explanatory, it is not complimented with explanatory texts and this may cause some problems for beginners to understand the code.&lt;br /&gt;
# One more thing to observe here is the font size! ( Not good for readers who are adapted to small or medium font size ).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://sourcemaking.com/design_patterns/adapter Adpater Pattern on Sourcemaking.com] ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Intent of Adapter Pattern, Explanation of the pattern, structure of the pattern, example, rule of thumb for adapter pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. This webpage gives a basic idea of adapter pattern and gives examples of the pattern in various programming languages.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Intent:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The need and purpose of adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Problem and Discussion:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
An “off the shelf” component offers compelling functionality that you would like to reuse, but its “view of the world” is not compatible with the philosophy and architecture of the system currently being developed. This section tells the problems and how adapter pattern helps solve them.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Structure:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Class diagrams showing the different structures of adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Example:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients. Socket wrenches provide an example of the Adapter. &amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Check list and Rule of thumb:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describe when and how adapter pattern is useful. It also describes the various things to keep in mind when working with adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Adapter Code examples:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Links to examples of Adapter pattern implemented in various languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; A good place to study the adapter pattern from scratch. The examples of code in different languages lets us compare the implementation across all these languages. The links contain easy to understand code making it simple for beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; Hmmm, now this webpage can be qualified as one of the better pages to learn about Adapter patterns. Since we couldn't find any shortcomings of this web page we leave it as an exercise for curious readers!&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== [http://www.blackwasp.co.uk/Adapter.aspx Adapter Pattern on blackwasp.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;What is adapter pattern, implementing adapter pattern, example of adapter pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;This page explained the adapter pattern in brief and then gives good example for implementing the adapter pattern and testing the adapter. The adapter pattern is a Gang of Four design pattern. This is a structural pattern as it defines a manner for creating relationships between classes. The adapter design pattern is used to provide a link between two otherwise incompatible types by wrapping the &amp;quot;adaptee&amp;quot; with a class that supports the interface required by the client.&lt;br /&gt;
* &amp;lt;b&amp;gt;What is adapter pattern:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The adapter pattern is introduced with the example of personnel system and intranet system. It helps build up the example in the implementation part&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
An implementation of the adapter with client, target, adaptee and adapter is shown.&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterForBlackWasp.png|thumb|350px|center|Adapter pattern implementation.]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Example and testing adapter:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The example of intranet and personnel system discussed in the introduction is implemented in C# code. Also how to test the adapter is shown.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This page is useful for beginners. It introduces an example and shows how to implement it in code. This implementation helps further understand the theory partin practical use.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; It focuses more on example than on the theory behind adapter pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Readings =&lt;br /&gt;
&lt;br /&gt;
[http://www.codeproject.com/Articles/342082/Understanding-and-Implementing-the-Adapter-Pattern Understanding and Implementing the Adapter Pattern].&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.allapplabs.com/java_design_patterns/adapter_pattern.htm Adapter Pattern in Java].&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groovy.codehaus.org/Adapter+Pattern Flavors of Adapter Pattern].&amp;lt;br&amp;gt;&lt;br /&gt;
[http://javapapers.com/design-patterns/adapter-pattern/ Implementing Adapter Pattern].&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:AdapterForBlackWasp.png&amp;diff=71113</id>
		<title>File:AdapterForBlackWasp.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:AdapterForBlackWasp.png&amp;diff=71113"/>
		<updated>2012-11-20T03:36:24Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: Reference:

http://www.blackwasp.co.uk/Adapter.aspx&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Reference:&lt;br /&gt;
&lt;br /&gt;
http://www.blackwasp.co.uk/Adapter.aspx&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=70156</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=70156"/>
		<updated>2012-11-18T10:00:12Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE_517_Fall_2012/Table_Of_Contents]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w37 ss]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w58 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w56 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w55 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w57 mp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w52 an]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch1b 1w38 nm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w60 ac]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w62 rb]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w29 st]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w3_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w30 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w17 pt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w31 up]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w9 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w19 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w26 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w16 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w8 vp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w3 jm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w23 sr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w11_aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w15 rr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w33 pv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w20_aa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w14_bb]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w21_ap]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w13_sm]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w4_sa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w25_nr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w12_sv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w7_ma]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w6_ar]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w32_mk]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w10_rc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w70_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w67_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w40_sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w22_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w-1w65_am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w59_bc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w60_ns]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b 2w47 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w69_as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w39_ka]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w36_av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w37_ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w43_iv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w53_iv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w63_sp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w49_ps]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w52_sj]]&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70152</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w47 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70152"/>
		<updated>2012-11-18T09:48:48Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Adpater Pattern on Codeproject.com */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Adapter Pattern, a directory of sites'''=&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Adapter Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Sometimes, objects just don't fit together as they should. A class may have changed, or an object turns out to be just too difficult to work with. At such times, adapter design pattern comes to the rescue. Adapter pattern lets you adapt what an object or class has to offer so that another object or class can make use of it. It also helps when classes can't work together because of incompatible interfaces.&lt;br /&gt;
&lt;br /&gt;
Since we are huge fans of visual approach, lets understand the adapter pattern with the help of a diagram. Hold on! Before going in to the UML diagram ( which is kind of less attractive, right? May be .. ) lets look at the figure below which is more appealing.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternForWiki.jpg|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;The above example helps readers quickly get across the &amp;lt;b&amp;gt;intent&amp;lt;/b&amp;gt; of the pattern than the UML diagram shown below. Looking at the UML diagram now, can help the readers to learn about the precise relationships between the classes in adapter pattern and it makes a lot more sense!&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternUMLForWiki.jpg|thumb|500px|center|UML diagram for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, this is all what we had to talk about Adapter Pattern. Lets look at other websites and see what they say about Adapter pattern, (which is probably, the main purpose of the wiki page).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Directory of sites =&lt;br /&gt;
&lt;br /&gt;
=== [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Pattern on wikipedia] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Lists various structures of Adapter Pattern viz., Object Adapter Pattern, Class Adapter Pattern, Runtime Adapter pattern and implementation of Adapter pattern. It provides a few examples for the above structures.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).&amp;lt;br&amp;gt; The various Adapter pattern structures are as follows&lt;br /&gt;
* &amp;lt;b&amp;gt;Object Adapter pattern:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Class Adapter pattern:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;A further form of runtime Adapter pattern:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides a good example for runtime Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Provides a basic skeleton of how to implement the adapter pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This wikipedia page works if readers want a quick review of the adapter pattern. Also it provides good references to read more about the Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; It doesn't helps to understand the Adapter pattern in depth. The examples provided are not easy to comprehend ( unless you are an experienced developer and not a beginner! ). Its not a good page to refer for the beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://msdn.microsoft.com/en-us/library/orm-9780596527730-01-04.aspx Adpater Pattern on MSDN Microsoft Website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Role of adapter pattern, Illustrations or examples where adapter pattern is used ,Design and Implementation of adapter pattern, few more kinds of adapters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter pattern is a versatile pattern that joins together types that were not designed to work with each other. It is one of those patterns that comes in useful when dealing with legacy code-i.e., code that was written a while ago and to which one might not have access.&lt;br /&gt;
* &amp;lt;b&amp;gt;Role:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern enables a system to use classes whose interfaces don't quite match its requirements. It is especially useful for off-the-shelf code, for toolkits, and for libraries&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Illustration:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Adapter pattern illustration-migration of Mac OS X from a 1998 PowerPC-based iMac to a 2007 Intel-based iMac. This section shows how the adapter pattern has been used to enable Mac OS migration.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Design:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern's important contribution is that it promotes programming to interfaces. The Client works to a domain-specific standard, which is specified in the ITarget interface. An Adaptee class provides the required functionality, but with a different interface. The Adapter implements the ITarget interface and routes calls from the Client through to the Adaptee, making whatever changes to parameters and return types are necessary to meet the requirements. A Target class that implements the ITarget interface directly could exist, but this is not a necessary part of the pattern. In any case, the Client is aware only of the ITarget interface, and it relies on that for its correct operation.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides an example in C# and how the Adapter, Adaptee, Target and Client all communicate with each other.&lt;br /&gt;
* &amp;lt;b&amp;gt;Two way adapters and Pluggable adapters:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describes the other kinds of adapter with appropriate examples.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This page gives an in-depth explanation of the adapter pattern. It shows how the adapter pattern is implemented in C#. The real world examples makes it easy to grasp the topic.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; The page might be too long for a quick review. Though it is really good for a beginner, an Intermediate or an expert person in design pattern may not actually want to wade through the entire concept in such a depth. ( unless the person doesn't have much work to do, &amp;quot;eyebrows raised&amp;quot; ).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.oodesign.com/adapter-pattern.html Adpater Pattern on OODesign.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Motivation for using Adapter pattern, intent behind using the adapter pattern, a UML diagram to explain the adapter pattern ( like we did above, yeah! ) to start with. It also covers the application and implementation of Adapter patterns. Moreover, it provides some really nice examples and specific problems where adapter pattern becomes a good fit to use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;The adapter pattern is adapting between classes and objects. Like any adapter in the real world it is used to be an interface, a bridge between two objects. In real world we have adapters for power supplies, adapters for camera memory cards, and so on.&amp;lt;br&amp;gt;&lt;br /&gt;
What about software development? It's the same. Can you imagine an situation when you have some class expecting some type of object and you have an object offering the same features, but exposing a different interface? Of course, you want to use both of them so you don't to implement again one of them, and you don't want to change existing classes, so why not create an adapter...&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Intent&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Convert the interface of a class into another interface clients expect&lt;br /&gt;
# Adapter lets classes work together, that could not otherwise because of incompatible interfaces&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Explains the implementation of the Adapter pattern with the help of a UML diagram.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Applicability and Examples&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Adapters are encountered everywhere. From real world adapters to software adapters.&lt;br /&gt;
# Non Software Examples of Adapter Patterns: Power Supply Adapters, card readers and adapters, ...&lt;br /&gt;
# Software Examples of Adapter Patterns: Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code. &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Specific problem and Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Objects Adapters - Based on Delegation&amp;lt;br&amp;gt; It uses composition, the Adaptee delegates the calls to Adaptee (opossed to class adapters which extends the Adaptee).&lt;br /&gt;
# Class Adapters - Based on (Multiple) Inheritance&amp;lt;br&amp;gt; Class adapters can be implemented in languages supporting multiple inheritance(Java, C# or PHP does not support multiple inheritance). Thus, such adapters can not be easy implemented in Java, C# or VB.NET. Class adapter uses inheritance instead of composition.&lt;br /&gt;
# Adapter Pattern and Strategy Pattern&amp;lt;br&amp;gt; Cases when Adapter Pattern can be a better option instead of Strategy pattern ( ok, now that sounds something interesting, doesn't it?)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; Helps to learn Adapter pattern in detail. Provides good examples to understand how the pattern works and also give suitable cases where adapter design pattern is most applicable. Good for all readers ( beginners, intermediate and experts )&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; Doesn't provides enough figures to understand the concept. For readers who endorse figures, not a good page to look out for.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.codeproject.com/Articles/42915/Adapter-Design-Pattern Adpater Pattern on Codeproject.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Background, Introduction to adapter pattern and its structure, class diagram and implementation of adapter pattern for a scenario.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;This page uses a running example in the background to discuss the design pattern and then further provides a design pattern implementation for that particular example. Moreover, it explains the importance of Adapter Design in system integration when other components have to be adapted by the existing system.&lt;br /&gt;
* &amp;lt;b&amp;gt;Background:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The webpage continues using the running example to describe the earlier design patterns and further gives a scenario to introduce the adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Design and implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
It gives a class diagram describing the adapter pattern for the example in the background section. Also, this section covers the explanation and implementation of all the classes and interfaces mentioned in the below class diagram:&lt;br /&gt;
[[File:AdapterClassOnCodeProject.JPG|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This website is useful for intermediate users of design patterns. Some preexisting knowledge of adapter pattern prior to reading this page can help to further understand the topic. The example provided gives a clear view of the design pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; A beginner might find not find this page a good base line to start. The code on the page might be difficult for a novice programmer to understand.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Further one more webpage on codeproject.com for beginners. [http://www.codeproject.com/Articles/186003/Adapter-Design-Pattern Adapter Design Pattern]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://csc.columbusstate.edu/woolbright/java/adapter.html Adpater Pattern on Columbus State University website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Introduction to Adapter pattern, Object-Oriented Adapters, an example which implements the Adapter pattern and real world adapters.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Intrduction:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; The adapter pattern changes the look of an interface. It takes an interface and adapts it to one the client is expecting.&lt;br /&gt;
   The Adapter Pattern converts the interface of a class into another interface the client expects. &lt;br /&gt;
   Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Object-Oriented Adapters:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Assume your existing system expects interface A.&lt;br /&gt;
# You need the system to work with a vendor class that has interface C.&lt;br /&gt;
# The solution is an adapter class with interface A for the existing system, and interface C for working with the Vendor class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;The Duck Interface Example:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; The website provides a good example of the Adapter Design Pattern. The example is as follows: &amp;lt;br&amp;gt; The code shown below provides the Duck Interface:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public interface Duck {&lt;br /&gt;
        public void quack();&lt;br /&gt;
        public void fly();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:It is implemented by the MallardDuck class as shown below:&lt;br /&gt;
&lt;br /&gt;
    public class MallardDuck implements Duck {&lt;br /&gt;
        public void quack() {&lt;br /&gt;
            System.out.println(&amp;quot;Quack&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
         &lt;br /&gt;
        public void fly() {&lt;br /&gt;
            System.out.println(&amp;quot;I'm flying&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:There is another interface called the Turkey Interface:&lt;br /&gt;
&lt;br /&gt;
    public interface Turkey {  //Turkeys don't quack&lt;br /&gt;
        public void gobble();&lt;br /&gt;
        public void fly();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:The turkey interface is implemented by the wildTurkey class as shown below:&lt;br /&gt;
&lt;br /&gt;
    public class WildTurkey implements Turkey {&lt;br /&gt;
        public void gobble() {&lt;br /&gt;
            System.out.println(&amp;quot;Gobble gobble&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
         &lt;br /&gt;
        public void fly() {&lt;br /&gt;
            System.out.println(&amp;quot;I'm flying a short distance&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:And finally comes our &amp;lt;i&amp;gt;showstopper&amp;lt;/i&amp;gt;, the Turkey Adapter code as shown below: &lt;br /&gt;
&lt;br /&gt;
    public class TurkeyAdapter implements Duck {&lt;br /&gt;
        Turkey turkey;&lt;br /&gt;
         &lt;br /&gt;
        public TurkeyAdapter(Turkey turkey) {&lt;br /&gt;
            this.turkey = turkey;&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        public void quack() {&lt;br /&gt;
            turkey.gobble();&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        public void fly() {&lt;br /&gt;
            for(int i=0; i &amp;lt; 5; i++) {&lt;br /&gt;
                turkey.fly();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:The website also provides a good tester to test the Adapter code. Please refer to the link to see the test driving code. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Real world Adapters:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Following are the examples of real world adapters:&lt;br /&gt;
# The old Java Enumeration class had a couple of methods:&lt;br /&gt;
## hasMoreElements()&lt;br /&gt;
## nextElement()&lt;br /&gt;
# The new Java Iterator class has three methods:&lt;br /&gt;
## hasNext()&lt;br /&gt;
## next()&lt;br /&gt;
## remove()&lt;br /&gt;
&lt;br /&gt;
This section provides an &amp;lt;i&amp;gt;EnumerationIterator&amp;lt;/i&amp;gt; adapter class that implements the Iterator. Please refer to the link to see the &amp;lt;i&amp;gt;EnumerationIterator&amp;lt;/i&amp;gt; Adapter class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# A very good example of Adapter Pattern using the Duck Interface, Turkey Interface and the TurkeyAdapter pattern. ( Gosh! Too much of a Turkey used, reminds of Thanksgiving! ).&lt;br /&gt;
# Also, the section on real world adapters compliments further understanding of Adapter pattern implementation. Good for Intermediate and Expert users of Design patterns&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Does not contains enough content to understand Adapter pattern from scratch.&lt;br /&gt;
# Also, not suitable for beginners. Though the code provided is lucid and self explanatory, it is not complimented with explanatory texts and this may cause some problems for beginners to understand the code.&lt;br /&gt;
# One more thing to observe here is the font size! ( Not good for readers who are adapted to small or medium font size ).&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://sourcemaking.com/design_patterns/adapter Adpater Pattern on Sourcemaking.com] ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Intent of Adapter Pattern, Explanation of the pattern, structure of the pattern, example, rule of thumb for adapter pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. This webpage gives a basic idea of adapter pattern and gives examples of the pattern in various programming languages.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Intent:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The need and purpose of adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Problem and Discussion:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
An “off the shelf” component offers compelling functionality that you would like to reuse, but its “view of the world” is not compatible with the philosophy and architecture of the system currently being developed. This section tells the problems and how adapter pattern helps solve them.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Structure:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Class diagrams showing the different structures of adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Example:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients. Socket wrenches provide an example of the Adapter. &amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Check list and Rule of thumb:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describe when and how adapter pattern is useful. It also describes the various things to keep in mind when working with adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Adapter Code examples:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Links to examples of Adapter pattern implemented in various languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; A good place to study the adapter pattern from scratch. The examples of code in different languages lets us compare the implementation across all these languages. The links contain easy to understand code making it simple for beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; Hmmm, now this webpage can be qualified as one of the better pages to learn about Adapter patterns. Since we couldn't find any shortcomings of this web page we leave it as an exercise for curious readers!&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Readings =&lt;br /&gt;
&lt;br /&gt;
[http://www.codeproject.com/Articles/342082/Understanding-and-Implementing-the-Adapter-Pattern Understanding and Implementing the Adapter Pattern].&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.allapplabs.com/java_design_patterns/adapter_pattern.htm Adapter Pattern in Java].&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groovy.codehaus.org/Adapter+Pattern Flavors of Adapter Pattern].&amp;lt;br&amp;gt;&lt;br /&gt;
[http://javapapers.com/design-patterns/adapter-pattern/ Implementing Adapter Pattern].&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70151</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w47 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70151"/>
		<updated>2012-11-18T09:48:13Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Adpater Pattern on Codeproject.com */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Adapter Pattern, a directory of sites'''=&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Adapter Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Sometimes, objects just don't fit together as they should. A class may have changed, or an object turns out to be just too difficult to work with. At such times, adapter design pattern comes to the rescue. Adapter pattern lets you adapt what an object or class has to offer so that another object or class can make use of it. It also helps when classes can't work together because of incompatible interfaces.&lt;br /&gt;
&lt;br /&gt;
Since we are huge fans of visual approach, lets understand the adapter pattern with the help of a diagram. Hold on! Before going in to the UML diagram ( which is kind of less attractive, right? May be .. ) lets look at the figure below which is more appealing.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternForWiki.jpg|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;The above example helps readers quickly get across the &amp;lt;b&amp;gt;intent&amp;lt;/b&amp;gt; of the pattern than the UML diagram shown below. Looking at the UML diagram now, can help the readers to learn about the precise relationships between the classes in adapter pattern and it makes a lot more sense!&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternUMLForWiki.jpg|thumb|500px|center|UML diagram for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, this is all what we had to talk about Adapter Pattern. Lets look at other websites and see what they say about Adapter pattern, (which is probably, the main purpose of the wiki page).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Directory of sites =&lt;br /&gt;
&lt;br /&gt;
=== [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Pattern on wikipedia] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Lists various structures of Adapter Pattern viz., Object Adapter Pattern, Class Adapter Pattern, Runtime Adapter pattern and implementation of Adapter pattern. It provides a few examples for the above structures.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).&amp;lt;br&amp;gt; The various Adapter pattern structures are as follows&lt;br /&gt;
* &amp;lt;b&amp;gt;Object Adapter pattern:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Class Adapter pattern:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;A further form of runtime Adapter pattern:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides a good example for runtime Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Provides a basic skeleton of how to implement the adapter pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This wikipedia page works if readers want a quick review of the adapter pattern. Also it provides good references to read more about the Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; It doesn't helps to understand the Adapter pattern in depth. The examples provided are not easy to comprehend ( unless you are an experienced developer and not a beginner! ). Its not a good page to refer for the beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://msdn.microsoft.com/en-us/library/orm-9780596527730-01-04.aspx Adpater Pattern on MSDN Microsoft Website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Role of adapter pattern, Illustrations or examples where adapter pattern is used ,Design and Implementation of adapter pattern, few more kinds of adapters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter pattern is a versatile pattern that joins together types that were not designed to work with each other. It is one of those patterns that comes in useful when dealing with legacy code-i.e., code that was written a while ago and to which one might not have access.&lt;br /&gt;
* &amp;lt;b&amp;gt;Role:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern enables a system to use classes whose interfaces don't quite match its requirements. It is especially useful for off-the-shelf code, for toolkits, and for libraries&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Illustration:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Adapter pattern illustration-migration of Mac OS X from a 1998 PowerPC-based iMac to a 2007 Intel-based iMac. This section shows how the adapter pattern has been used to enable Mac OS migration.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Design:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern's important contribution is that it promotes programming to interfaces. The Client works to a domain-specific standard, which is specified in the ITarget interface. An Adaptee class provides the required functionality, but with a different interface. The Adapter implements the ITarget interface and routes calls from the Client through to the Adaptee, making whatever changes to parameters and return types are necessary to meet the requirements. A Target class that implements the ITarget interface directly could exist, but this is not a necessary part of the pattern. In any case, the Client is aware only of the ITarget interface, and it relies on that for its correct operation.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides an example in C# and how the Adapter, Adaptee, Target and Client all communicate with each other.&lt;br /&gt;
* &amp;lt;b&amp;gt;Two way adapters and Pluggable adapters:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describes the other kinds of adapter with appropriate examples.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This page gives an in-depth explanation of the adapter pattern. It shows how the adapter pattern is implemented in C#. The real world examples makes it easy to grasp the topic.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; The page might be too long for a quick review. Though it is really good for a beginner, an Intermediate or an expert person in design pattern may not actually want to wade through the entire concept in such a depth. ( unless the person doesn't have much work to do, &amp;quot;eyebrows raised&amp;quot; ).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.oodesign.com/adapter-pattern.html Adpater Pattern on OODesign.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Motivation for using Adapter pattern, intent behind using the adapter pattern, a UML diagram to explain the adapter pattern ( like we did above, yeah! ) to start with. It also covers the application and implementation of Adapter patterns. Moreover, it provides some really nice examples and specific problems where adapter pattern becomes a good fit to use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;The adapter pattern is adapting between classes and objects. Like any adapter in the real world it is used to be an interface, a bridge between two objects. In real world we have adapters for power supplies, adapters for camera memory cards, and so on.&amp;lt;br&amp;gt;&lt;br /&gt;
What about software development? It's the same. Can you imagine an situation when you have some class expecting some type of object and you have an object offering the same features, but exposing a different interface? Of course, you want to use both of them so you don't to implement again one of them, and you don't want to change existing classes, so why not create an adapter...&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Intent&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Convert the interface of a class into another interface clients expect&lt;br /&gt;
# Adapter lets classes work together, that could not otherwise because of incompatible interfaces&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Explains the implementation of the Adapter pattern with the help of a UML diagram.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Applicability and Examples&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Adapters are encountered everywhere. From real world adapters to software adapters.&lt;br /&gt;
# Non Software Examples of Adapter Patterns: Power Supply Adapters, card readers and adapters, ...&lt;br /&gt;
# Software Examples of Adapter Patterns: Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code. &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Specific problem and Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Objects Adapters - Based on Delegation&amp;lt;br&amp;gt; It uses composition, the Adaptee delegates the calls to Adaptee (opossed to class adapters which extends the Adaptee).&lt;br /&gt;
# Class Adapters - Based on (Multiple) Inheritance&amp;lt;br&amp;gt; Class adapters can be implemented in languages supporting multiple inheritance(Java, C# or PHP does not support multiple inheritance). Thus, such adapters can not be easy implemented in Java, C# or VB.NET. Class adapter uses inheritance instead of composition.&lt;br /&gt;
# Adapter Pattern and Strategy Pattern&amp;lt;br&amp;gt; Cases when Adapter Pattern can be a better option instead of Strategy pattern ( ok, now that sounds something interesting, doesn't it?)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; Helps to learn Adapter pattern in detail. Provides good examples to understand how the pattern works and also give suitable cases where adapter design pattern is most applicable. Good for all readers ( beginners, intermediate and experts )&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; Doesn't provides enough figures to understand the concept. For readers who endorse figures, not a good page to look out for.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.codeproject.com/Articles/42915/Adapter-Design-Pattern Adpater Pattern on Codeproject.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Background, Introduction to adapter pattern and its structure, class diagram and implementation of adapter pattern for a scenario.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;This page uses a running example in the background to discuss the design pattern and then further provides a design pattern implementation for that particular example. Moreover, it explains the importance of Adapter Design in system integration when other components have to be adapted by the existing system.&lt;br /&gt;
* &amp;lt;b&amp;gt;Background:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The webpage continues using the running example to describe the earlier design patterns and further gives a scenario to introduce the adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Design and implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
It gives a class diagram describing the adapter pattern for the example in the background section. Also, this section covers the explanation and implementation of all the classes and interfaces mentioned in the below class diagram:&lt;br /&gt;
[[File:AdapterClassOnCodeProject.JPG|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This website is useful for intermediate users of design patterns. Some preexisting knowledge of adapter pattern prior to reading this page can help to further understand the topic. The example provided gives a clear view of the design pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; A beginner might find not find this page a good base line to start. The code on the page might be difficult for a novice programmer to understand.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Further one more site on codeproject.com for beginners. [http://www.codeproject.com/Articles/186003/Adapter-Design-Pattern Adapter Design Pattern]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://csc.columbusstate.edu/woolbright/java/adapter.html Adpater Pattern on Columbus State University website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Introduction to Adapter pattern, Object-Oriented Adapters, an example which implements the Adapter pattern and real world adapters.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Intrduction:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; The adapter pattern changes the look of an interface. It takes an interface and adapts it to one the client is expecting.&lt;br /&gt;
   The Adapter Pattern converts the interface of a class into another interface the client expects. &lt;br /&gt;
   Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Object-Oriented Adapters:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Assume your existing system expects interface A.&lt;br /&gt;
# You need the system to work with a vendor class that has interface C.&lt;br /&gt;
# The solution is an adapter class with interface A for the existing system, and interface C for working with the Vendor class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;The Duck Interface Example:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; The website provides a good example of the Adapter Design Pattern. The example is as follows: &amp;lt;br&amp;gt; The code shown below provides the Duck Interface:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public interface Duck {&lt;br /&gt;
        public void quack();&lt;br /&gt;
        public void fly();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:It is implemented by the MallardDuck class as shown below:&lt;br /&gt;
&lt;br /&gt;
    public class MallardDuck implements Duck {&lt;br /&gt;
        public void quack() {&lt;br /&gt;
            System.out.println(&amp;quot;Quack&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
         &lt;br /&gt;
        public void fly() {&lt;br /&gt;
            System.out.println(&amp;quot;I'm flying&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:There is another interface called the Turkey Interface:&lt;br /&gt;
&lt;br /&gt;
    public interface Turkey {  //Turkeys don't quack&lt;br /&gt;
        public void gobble();&lt;br /&gt;
        public void fly();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:The turkey interface is implemented by the wildTurkey class as shown below:&lt;br /&gt;
&lt;br /&gt;
    public class WildTurkey implements Turkey {&lt;br /&gt;
        public void gobble() {&lt;br /&gt;
            System.out.println(&amp;quot;Gobble gobble&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
         &lt;br /&gt;
        public void fly() {&lt;br /&gt;
            System.out.println(&amp;quot;I'm flying a short distance&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:And finally comes our &amp;lt;i&amp;gt;showstopper&amp;lt;/i&amp;gt;, the Turkey Adapter code as shown below: &lt;br /&gt;
&lt;br /&gt;
    public class TurkeyAdapter implements Duck {&lt;br /&gt;
        Turkey turkey;&lt;br /&gt;
         &lt;br /&gt;
        public TurkeyAdapter(Turkey turkey) {&lt;br /&gt;
            this.turkey = turkey;&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        public void quack() {&lt;br /&gt;
            turkey.gobble();&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        public void fly() {&lt;br /&gt;
            for(int i=0; i &amp;lt; 5; i++) {&lt;br /&gt;
                turkey.fly();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
:The website also provides a good tester to test the Adapter code. Please refer to the link to see the test driving code. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Real world Adapters:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Following are the examples of real world adapters:&lt;br /&gt;
# The old Java Enumeration class had a couple of methods:&lt;br /&gt;
## hasMoreElements()&lt;br /&gt;
## nextElement()&lt;br /&gt;
# The new Java Iterator class has three methods:&lt;br /&gt;
## hasNext()&lt;br /&gt;
## next()&lt;br /&gt;
## remove()&lt;br /&gt;
&lt;br /&gt;
This section provides an &amp;lt;i&amp;gt;EnumerationIterator&amp;lt;/i&amp;gt; adapter class that implements the Iterator. Please refer to the link to see the &amp;lt;i&amp;gt;EnumerationIterator&amp;lt;/i&amp;gt; Adapter class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# A very good example of Adapter Pattern using the Duck Interface, Turkey Interface and the TurkeyAdapter pattern. ( Gosh! Too much of a Turkey used, reminds of Thanksgiving! ).&lt;br /&gt;
# Also, the section on real world adapters compliments further understanding of Adapter pattern implementation. Good for Intermediate and Expert users of Design patterns&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Does not contains enough content to understand Adapter pattern from scratch.&lt;br /&gt;
# Also, not suitable for beginners. Though the code provided is lucid and self explanatory, it is not complimented with explanatory texts and this may cause some problems for beginners to understand the code.&lt;br /&gt;
# One more thing to observe here is the font size! ( Not good for readers who are adapted to small or medium font size ).&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://sourcemaking.com/design_patterns/adapter Adpater Pattern on Sourcemaking.com] ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Intent of Adapter Pattern, Explanation of the pattern, structure of the pattern, example, rule of thumb for adapter pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. This webpage gives a basic idea of adapter pattern and gives examples of the pattern in various programming languages.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Intent:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The need and purpose of adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Problem and Discussion:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
An “off the shelf” component offers compelling functionality that you would like to reuse, but its “view of the world” is not compatible with the philosophy and architecture of the system currently being developed. This section tells the problems and how adapter pattern helps solve them.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Structure:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Class diagrams showing the different structures of adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Example:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients. Socket wrenches provide an example of the Adapter. &amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Check list and Rule of thumb:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describe when and how adapter pattern is useful. It also describes the various things to keep in mind when working with adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Adapter Code examples:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Links to examples of Adapter pattern implemented in various languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; A good place to study the adapter pattern from scratch. The examples of code in different languages lets us compare the implementation across all these languages. The links contain easy to understand code making it simple for beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; Hmmm, now this webpage can be qualified as one of the better pages to learn about Adapter patterns. Since we couldn't find any shortcomings of this web page we leave it as an exercise for curious readers!&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Readings =&lt;br /&gt;
&lt;br /&gt;
[http://www.codeproject.com/Articles/342082/Understanding-and-Implementing-the-Adapter-Pattern Understanding and Implementing the Adapter Pattern].&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.allapplabs.com/java_design_patterns/adapter_pattern.htm Adapter Pattern in Java].&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groovy.codehaus.org/Adapter+Pattern Flavors of Adapter Pattern].&amp;lt;br&amp;gt;&lt;br /&gt;
[http://javapapers.com/design-patterns/adapter-pattern/ Implementing Adapter Pattern].&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70132</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w47 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70132"/>
		<updated>2012-11-18T08:55:11Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Further Readings */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Adapter Pattern, a directory of sites'''=&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Adapter Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Sometimes, objects just don't fit together as they should. A class may have changed, or an object turns out to be just too difficult to work with. At such times, adapter design pattern comes to the rescue. Adapter pattern lets you adapt what an object or class has to offer so that another object or class can make use of it. It also helps when classes can't work together because of incompatible interfaces.&lt;br /&gt;
&lt;br /&gt;
Since we are huge fans of visual approach, lets understand the adapter pattern with the help of a diagram. Hold on! Before going in to the UML diagram ( which is kind of less attractive, right? May be .. ) lets look at the figure below which is more appealing.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternForWiki.jpg|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;The above example helps readers quickly get across the &amp;lt;b&amp;gt;intent&amp;lt;/b&amp;gt; of the pattern than the UML diagram shown below. Looking at the UML diagram now, can help the readers to learn about the precise relationships between the classes in adapter pattern and it makes a lot more sense!&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternUMLForWiki.jpg|thumb|500px|center|UML diagram for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, this is all what we had to talk about Adapter Pattern. Lets look at other websites and see what they say about Adapter pattern, (which is probably, the main purpose of the wiki page).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Directory of sites =&lt;br /&gt;
&lt;br /&gt;
=== [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Pattern on wikipedia] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Lists various structures of Adapter Pattern viz., Object Adapter Pattern, Class Adapter Pattern, Runtime Adapter pattern and implementation of Adapter pattern. It provides a few examples for the above structures.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).&amp;lt;br&amp;gt; The various Adapter pattern structures are as follows&lt;br /&gt;
* Object Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.&amp;lt;br&amp;gt;&lt;br /&gt;
* Class Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing.&amp;lt;br&amp;gt;&lt;br /&gt;
* A further form of runtime Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides a good example for runtime Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Implementation:&amp;lt;br&amp;gt;&lt;br /&gt;
Provides a basic skeleton of how to implement the adapter pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This wikipedia page works if readers want a quick review of the adapter pattern. Also it provides good references to read more about the Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; It doesn't helps to understand the Adapter pattern in depth. The examples provided are not easy to comprehend ( unless you are an experienced developer and not a beginner! ). Its not a good page to refer for the beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://msdn.microsoft.com/en-us/library/orm-9780596527730-01-04.aspx Adpater Pattern on MSDN Microsoft Website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Role of adapter pattern, Illustrations or examples where adapter pattern is used ,Design and Implementation of adapter pattern, few more kinds of adapters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter pattern is a versatile pattern that joins together types that were not designed to work with each other. It is one of those patterns that comes in useful when dealing with legacy code-i.e., code that was written a while ago and to which one might not have access.&lt;br /&gt;
* Role:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern enables a system to use classes whose interfaces don't quite match its requirements. It is especially useful for off-the-shelf code, for toolkits, and for libraries&amp;lt;br&amp;gt;&lt;br /&gt;
* Illustration:&amp;lt;br&amp;gt;&lt;br /&gt;
Adapter pattern illustration-migration of Mac OS X from a 1998 PowerPC-based iMac to a 2007 Intel-based iMac. This section shows how the adapter pattern has been used to enable Mac OS migration.&amp;lt;br&amp;gt;&lt;br /&gt;
* Design:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern's important contribution is that it promotes programming to interfaces. The Client works to a domain-specific standard, which is specified in the ITarget interface. An Adaptee class provides the required functionality, but with a different interface. The Adapter implements the ITarget interface and routes calls from the Client through to the Adaptee, making whatever changes to parameters and return types are necessary to meet the requirements. A Target class that implements the ITarget interface directly could exist, but this is not a necessary part of the pattern. In any case, the Client is aware only of the ITarget interface, and it relies on that for its correct operation.&amp;lt;br&amp;gt;&lt;br /&gt;
* Implementation:&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides an example in C# and how the Adapter, Adaptee, Target and Client all communicate with each other.&lt;br /&gt;
* Two way adapters and Pluggable adapters:&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describes the other kinds of adapter with appropriate examples.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This page gives an in-depth explanation of the adapter pattern. It shows how the adapter pattern is implemented in C#. The real world examples makes it easy to grasp the topic.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; The page might be too long for a quick review. Though it is really good for a beginner, an Intermediate or an expert person in design pattern may not actually want to wade through the entire concept in such a depth. ( unless the person doesn't have much work to do, &amp;quot;eyebrows raised&amp;quot; ).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.oodesign.com/adapter-pattern.html Adpater Pattern on OODesign.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Motivation for using Adapter pattern, intent behind using the adapter pattern, a UML diagram to explain the adapter pattern ( like we did above, yeah! ) to start with. It also covers the application and implementation of Adapter patterns. Moreover, it provides some really nice examples and specific problems where adapter pattern becomes a good fit to use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;The adapter pattern is adapting between classes and objects. Like any adapter in the real world it is used to be an interface, a bridge between two objects. In real world we have adapters for power supplies, adapters for camera memory cards, and so on.&amp;lt;br&amp;gt;&lt;br /&gt;
What about software development? It's the same. Can you imagine an situation when you have some class expecting some type of object and you have an object offering the same features, but exposing a different interface? Of course, you want to use both of them so you don't to implement again one of them, and you don't want to change existing classes, so why not create an adapter...&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Intent&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Convert the interface of a class into another interface clients expect&lt;br /&gt;
# Adapter lets classes work together, that could not otherwise because of incompatible interfaces&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Explains the implementation of the Adapter pattern with the help of a UML diagram.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Applicability and Examples&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Adapters are encountered everywhere. From real world adapters to software adapters.&lt;br /&gt;
# Non Software Examples of Adapter Patterns: Power Supply Adapters, card readers and adapters, ...&lt;br /&gt;
# Software Examples of Adapter Patterns: Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code. &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Specific problem and Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Objects Adapters - Based on Delegation&amp;lt;br&amp;gt; It uses composition, the Adaptee delegates the calls to Adaptee (opossed to class adapters which extends the Adaptee).&lt;br /&gt;
# Class Adapters - Based on (Multiple) Inheritance&amp;lt;br&amp;gt; Class adapters can be implemented in languages supporting multiple inheritance(Java, C# or PHP does not support multiple inheritance). Thus, such adapters can not be easy implemented in Java, C# or VB.NET. Class adapter uses inheritance instead of composition.&lt;br /&gt;
# Adapter Pattern and Strategy Pattern&amp;lt;br&amp;gt; Cases when Adapter Pattern can be a better option instead of Strategy pattern ( ok, now that sounds something interesting, doesn't it?)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; Helps to learn Adapter pattern in detail. Provides good examples to understand how the pattern works and also give suitable cases where adapter design pattern is most applicable. Good for all readers ( beginners, intermediate and experts )&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; Doesn't provides enough figures to understand the concept. For readers who endorse figures, not a good page to look out for.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.codeproject.com/Articles/42915/Adapter-Design-Pattern Adpater Pattern on Codeproject.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Background, Introduction to adapter pattern and its structure, class diagram and implementation of adapter pattern for a scenario.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;This page uses a running example in the background to discuss the design pattern and then further provides a design pattern implementation for that particular example. Moreover, it explains the importance of Adapter Design in system integration when other components have to be adapted by the existing system.&lt;br /&gt;
* Background:&amp;lt;br&amp;gt;&lt;br /&gt;
The webpage continues using the running example to describe the earlier design patterns and further gives a scenario to introduce the adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Design and implementation&amp;lt;br&amp;gt;&lt;br /&gt;
It gives a class diagram describing the adapter pattern for the example in the background section. Also, this section covers the explanation and implementation of all the classes and interfaces mentioned in the below class diagram:&lt;br /&gt;
[[File:AdapterClassOnCodeProject.JPG|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This website is useful for intermediate users of design patterns. Some preexisting knowledge of adapter pattern prior to reading this page can help to further understand the topic. The example provided gives a clear view of the design pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; A beginner might find not find this page a good base line to start. The code on the page might be difficult for a novice programmer to understand.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://csc.columbusstate.edu/woolbright/java/adapter.html Adpater Pattern on Columbus State University website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Introduction to Adapter pattern, Object-Oriented Adapters, an example which implements the Adapter pattern and real world adapters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* Intrduction:&amp;lt;br&amp;gt; The adapter pattern changes the look of an interface. It takes an interface and adapts it to one the client is expecting.&lt;br /&gt;
   The Adapter Pattern converts the interface of a class into another interface the client expects. &lt;br /&gt;
   Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== [http://sourcemaking.com/design_patterns/adapter Adpater Pattern on Sourcemaking.com] ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Intent of Adapter Pattern, Explanation of the pattern, structure of the pattern, example, rule of thumb for adapter pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. This webpage gives a basic idea of adapter pattern and gives examples of the pattern in various programming languages.&lt;br /&gt;
&lt;br /&gt;
* Intent:&amp;lt;br&amp;gt;&lt;br /&gt;
The need and purpose of adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Problem and Discussion:&amp;lt;br&amp;gt;&lt;br /&gt;
An “off the shelf” component offers compelling functionality that you would like to reuse, but its “view of the world” is not compatible with the philosophy and architecture of the system currently being developed. This section tells the problems and how adapter pattern helps solve them.&amp;lt;br&amp;gt;&lt;br /&gt;
* Structure:&amp;lt;br&amp;gt;&lt;br /&gt;
Class diagrams showing the different structures of adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Example:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients. Socket wrenches provide an example of the Adapter. &amp;lt;br&amp;gt;&lt;br /&gt;
* Check list and Rule of thumb:&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describe when and how adapter pattern is useful. It also describes the various things to keep in mind when working with adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Adapter Code examples:&amp;lt;br&amp;gt;&lt;br /&gt;
Links to examples of Adapter pattern implemented in various languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; A good place to study the adapter pattern from scratch. The examples of code in different languages lets us compare the implementation across all these languages. The links contain easy to understand code making it simple for beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; Hmmm, now this webpage can be qualified as one of the better pages to learn about Adapter patterns. Since we couldn't find any shortcomings of this web page we leave it as an exercise for curious readers!&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Readings =&lt;br /&gt;
&lt;br /&gt;
[http://www.codeproject.com/Articles/342082/Understanding-and-Implementing-the-Adapter-Pattern Understanding and Implementing the Adapter Pattern].&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.allapplabs.com/java_design_patterns/adapter_pattern.htm Adapter Pattern in Java].&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groovy.codehaus.org/Adapter+Pattern Flavors of Adapter Pattern].&amp;lt;br&amp;gt;&lt;br /&gt;
[http://javapapers.com/design-patterns/adapter-pattern/ Implementing Adapter Pattern].&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70131</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w47 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70131"/>
		<updated>2012-11-18T08:54:50Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Further Readings */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Adapter Pattern, a directory of sites'''=&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Adapter Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Sometimes, objects just don't fit together as they should. A class may have changed, or an object turns out to be just too difficult to work with. At such times, adapter design pattern comes to the rescue. Adapter pattern lets you adapt what an object or class has to offer so that another object or class can make use of it. It also helps when classes can't work together because of incompatible interfaces.&lt;br /&gt;
&lt;br /&gt;
Since we are huge fans of visual approach, lets understand the adapter pattern with the help of a diagram. Hold on! Before going in to the UML diagram ( which is kind of less attractive, right? May be .. ) lets look at the figure below which is more appealing.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternForWiki.jpg|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;The above example helps readers quickly get across the &amp;lt;b&amp;gt;intent&amp;lt;/b&amp;gt; of the pattern than the UML diagram shown below. Looking at the UML diagram now, can help the readers to learn about the precise relationships between the classes in adapter pattern and it makes a lot more sense!&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternUMLForWiki.jpg|thumb|500px|center|UML diagram for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, this is all what we had to talk about Adapter Pattern. Lets look at other websites and see what they say about Adapter pattern, (which is probably, the main purpose of the wiki page).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Directory of sites =&lt;br /&gt;
&lt;br /&gt;
=== [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Pattern on wikipedia] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Lists various structures of Adapter Pattern viz., Object Adapter Pattern, Class Adapter Pattern, Runtime Adapter pattern and implementation of Adapter pattern. It provides a few examples for the above structures.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).&amp;lt;br&amp;gt; The various Adapter pattern structures are as follows&lt;br /&gt;
* Object Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.&amp;lt;br&amp;gt;&lt;br /&gt;
* Class Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing.&amp;lt;br&amp;gt;&lt;br /&gt;
* A further form of runtime Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides a good example for runtime Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Implementation:&amp;lt;br&amp;gt;&lt;br /&gt;
Provides a basic skeleton of how to implement the adapter pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This wikipedia page works if readers want a quick review of the adapter pattern. Also it provides good references to read more about the Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; It doesn't helps to understand the Adapter pattern in depth. The examples provided are not easy to comprehend ( unless you are an experienced developer and not a beginner! ). Its not a good page to refer for the beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://msdn.microsoft.com/en-us/library/orm-9780596527730-01-04.aspx Adpater Pattern on MSDN Microsoft Website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Role of adapter pattern, Illustrations or examples where adapter pattern is used ,Design and Implementation of adapter pattern, few more kinds of adapters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter pattern is a versatile pattern that joins together types that were not designed to work with each other. It is one of those patterns that comes in useful when dealing with legacy code-i.e., code that was written a while ago and to which one might not have access.&lt;br /&gt;
* Role:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern enables a system to use classes whose interfaces don't quite match its requirements. It is especially useful for off-the-shelf code, for toolkits, and for libraries&amp;lt;br&amp;gt;&lt;br /&gt;
* Illustration:&amp;lt;br&amp;gt;&lt;br /&gt;
Adapter pattern illustration-migration of Mac OS X from a 1998 PowerPC-based iMac to a 2007 Intel-based iMac. This section shows how the adapter pattern has been used to enable Mac OS migration.&amp;lt;br&amp;gt;&lt;br /&gt;
* Design:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern's important contribution is that it promotes programming to interfaces. The Client works to a domain-specific standard, which is specified in the ITarget interface. An Adaptee class provides the required functionality, but with a different interface. The Adapter implements the ITarget interface and routes calls from the Client through to the Adaptee, making whatever changes to parameters and return types are necessary to meet the requirements. A Target class that implements the ITarget interface directly could exist, but this is not a necessary part of the pattern. In any case, the Client is aware only of the ITarget interface, and it relies on that for its correct operation.&amp;lt;br&amp;gt;&lt;br /&gt;
* Implementation:&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides an example in C# and how the Adapter, Adaptee, Target and Client all communicate with each other.&lt;br /&gt;
* Two way adapters and Pluggable adapters:&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describes the other kinds of adapter with appropriate examples.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This page gives an in-depth explanation of the adapter pattern. It shows how the adapter pattern is implemented in C#. The real world examples makes it easy to grasp the topic.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; The page might be too long for a quick review. Though it is really good for a beginner, an Intermediate or an expert person in design pattern may not actually want to wade through the entire concept in such a depth. ( unless the person doesn't have much work to do, &amp;quot;eyebrows raised&amp;quot; ).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.oodesign.com/adapter-pattern.html Adpater Pattern on OODesign.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Motivation for using Adapter pattern, intent behind using the adapter pattern, a UML diagram to explain the adapter pattern ( like we did above, yeah! ) to start with. It also covers the application and implementation of Adapter patterns. Moreover, it provides some really nice examples and specific problems where adapter pattern becomes a good fit to use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;The adapter pattern is adapting between classes and objects. Like any adapter in the real world it is used to be an interface, a bridge between two objects. In real world we have adapters for power supplies, adapters for camera memory cards, and so on.&amp;lt;br&amp;gt;&lt;br /&gt;
What about software development? It's the same. Can you imagine an situation when you have some class expecting some type of object and you have an object offering the same features, but exposing a different interface? Of course, you want to use both of them so you don't to implement again one of them, and you don't want to change existing classes, so why not create an adapter...&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Intent&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Convert the interface of a class into another interface clients expect&lt;br /&gt;
# Adapter lets classes work together, that could not otherwise because of incompatible interfaces&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Explains the implementation of the Adapter pattern with the help of a UML diagram.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Applicability and Examples&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Adapters are encountered everywhere. From real world adapters to software adapters.&lt;br /&gt;
# Non Software Examples of Adapter Patterns: Power Supply Adapters, card readers and adapters, ...&lt;br /&gt;
# Software Examples of Adapter Patterns: Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code. &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Specific problem and Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Objects Adapters - Based on Delegation&amp;lt;br&amp;gt; It uses composition, the Adaptee delegates the calls to Adaptee (opossed to class adapters which extends the Adaptee).&lt;br /&gt;
# Class Adapters - Based on (Multiple) Inheritance&amp;lt;br&amp;gt; Class adapters can be implemented in languages supporting multiple inheritance(Java, C# or PHP does not support multiple inheritance). Thus, such adapters can not be easy implemented in Java, C# or VB.NET. Class adapter uses inheritance instead of composition.&lt;br /&gt;
# Adapter Pattern and Strategy Pattern&amp;lt;br&amp;gt; Cases when Adapter Pattern can be a better option instead of Strategy pattern ( ok, now that sounds something interesting, doesn't it?)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; Helps to learn Adapter pattern in detail. Provides good examples to understand how the pattern works and also give suitable cases where adapter design pattern is most applicable. Good for all readers ( beginners, intermediate and experts )&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; Doesn't provides enough figures to understand the concept. For readers who endorse figures, not a good page to look out for.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.codeproject.com/Articles/42915/Adapter-Design-Pattern Adpater Pattern on Codeproject.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Background, Introduction to adapter pattern and its structure, class diagram and implementation of adapter pattern for a scenario.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;This page uses a running example in the background to discuss the design pattern and then further provides a design pattern implementation for that particular example. Moreover, it explains the importance of Adapter Design in system integration when other components have to be adapted by the existing system.&lt;br /&gt;
* Background:&amp;lt;br&amp;gt;&lt;br /&gt;
The webpage continues using the running example to describe the earlier design patterns and further gives a scenario to introduce the adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Design and implementation&amp;lt;br&amp;gt;&lt;br /&gt;
It gives a class diagram describing the adapter pattern for the example in the background section. Also, this section covers the explanation and implementation of all the classes and interfaces mentioned in the below class diagram:&lt;br /&gt;
[[File:AdapterClassOnCodeProject.JPG|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This website is useful for intermediate users of design patterns. Some preexisting knowledge of adapter pattern prior to reading this page can help to further understand the topic. The example provided gives a clear view of the design pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; A beginner might find not find this page a good base line to start. The code on the page might be difficult for a novice programmer to understand.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://csc.columbusstate.edu/woolbright/java/adapter.html Adpater Pattern on Columbus State University website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Introduction to Adapter pattern, Object-Oriented Adapters, an example which implements the Adapter pattern and real world adapters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* Intrduction:&amp;lt;br&amp;gt; The adapter pattern changes the look of an interface. It takes an interface and adapts it to one the client is expecting.&lt;br /&gt;
   The Adapter Pattern converts the interface of a class into another interface the client expects. &lt;br /&gt;
   Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== [http://sourcemaking.com/design_patterns/adapter Adpater Pattern on Sourcemaking.com] ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Intent of Adapter Pattern, Explanation of the pattern, structure of the pattern, example, rule of thumb for adapter pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. This webpage gives a basic idea of adapter pattern and gives examples of the pattern in various programming languages.&lt;br /&gt;
&lt;br /&gt;
* Intent:&amp;lt;br&amp;gt;&lt;br /&gt;
The need and purpose of adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Problem and Discussion:&amp;lt;br&amp;gt;&lt;br /&gt;
An “off the shelf” component offers compelling functionality that you would like to reuse, but its “view of the world” is not compatible with the philosophy and architecture of the system currently being developed. This section tells the problems and how adapter pattern helps solve them.&amp;lt;br&amp;gt;&lt;br /&gt;
* Structure:&amp;lt;br&amp;gt;&lt;br /&gt;
Class diagrams showing the different structures of adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Example:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients. Socket wrenches provide an example of the Adapter. &amp;lt;br&amp;gt;&lt;br /&gt;
* Check list and Rule of thumb:&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describe when and how adapter pattern is useful. It also describes the various things to keep in mind when working with adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Adapter Code examples:&amp;lt;br&amp;gt;&lt;br /&gt;
Links to examples of Adapter pattern implemented in various languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; A good place to study the adapter pattern from scratch. The examples of code in different languages lets us compare the implementation across all these languages. The links contain easy to understand code making it simple for beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; Hmmm, now this webpage can be qualified as one of the better pages to learn about Adapter patterns. Since we couldn't find any shortcomings of this web page we leave it as an exercise for curious readers!&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Readings =&lt;br /&gt;
&lt;br /&gt;
[http://www.codeproject.com/Articles/342082/Understanding-and-Implementing-the-Adapter-Pattern Understanding and Implementing the Adapter Pattern].&lt;br /&gt;
[http://www.allapplabs.com/java_design_patterns/adapter_pattern.htm Adapter Pattern in Java].&lt;br /&gt;
[http://groovy.codehaus.org/Adapter+Pattern Flavors of Adapter Pattern].&lt;br /&gt;
[http://javapapers.com/design-patterns/adapter-pattern/ Implementing Adapter Pattern].&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70127</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w47 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70127"/>
		<updated>2012-11-18T08:45:10Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Adapter Pattern, a directory of sites'''=&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Adapter Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Sometimes, objects just don't fit together as they should. A class may have changed, or an object turns out to be just too difficult to work with. At such times, adapter design pattern comes to the rescue. Adapter pattern lets you adapt what an object or class has to offer so that another object or class can make use of it. It also helps when classes can't work together because of incompatible interfaces.&lt;br /&gt;
&lt;br /&gt;
Since we are huge fans of visual approach, lets understand the adapter pattern with the help of a diagram. Hold on! Before going in to the UML diagram ( which is kind of less attractive, right? May be .. ) lets look at the figure below which is more appealing.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternForWiki.jpg|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;The above example helps readers quickly get across the &amp;lt;b&amp;gt;intent&amp;lt;/b&amp;gt; of the pattern than the UML diagram shown below. Looking at the UML diagram now, can help the readers to learn about the precise relationships between the classes in adapter pattern and it makes a lot more sense!&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternUMLForWiki.jpg|thumb|500px|center|UML diagram for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, this is all what we had to talk about Adapter Pattern. Lets look at other websites and see what they say about Adapter pattern, (which is probably, the main purpose of the wiki page).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Directory of sites =&lt;br /&gt;
&lt;br /&gt;
=== [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Pattern on wikipedia] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Lists various structures of Adapter Pattern viz., Object Adapter Pattern, Class Adapter Pattern, Runtime Adapter pattern and implementation of Adapter pattern. It provides a few examples for the above structures.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).&amp;lt;br&amp;gt; The various Adapter pattern structures are as follows&lt;br /&gt;
* Object Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.&amp;lt;br&amp;gt;&lt;br /&gt;
* Class Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing.&amp;lt;br&amp;gt;&lt;br /&gt;
* A further form of runtime Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides a good example for runtime Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Implementation:&amp;lt;br&amp;gt;&lt;br /&gt;
Provides a basic skeleton of how to implement the adapter pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This wikipedia page works if readers want a quick review of the adapter pattern. Also it provides good references to read more about the Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; It doesn't helps to understand the Adapter pattern in depth. The examples provided are not easy to comprehend ( unless you are an experienced developer and not a beginner! ). Its not a good page to refer for the beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://msdn.microsoft.com/en-us/library/orm-9780596527730-01-04.aspx Adpater Pattern on MSDN Microsoft Website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Role of adapter pattern, Illustrations or examples where adapter pattern is used ,Design and Implementation of adapter pattern, few more kinds of adapters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter pattern is a versatile pattern that joins together types that were not designed to work with each other. It is one of those patterns that comes in useful when dealing with legacy code-i.e., code that was written a while ago and to which one might not have access.&lt;br /&gt;
* Role:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern enables a system to use classes whose interfaces don't quite match its requirements. It is especially useful for off-the-shelf code, for toolkits, and for libraries&amp;lt;br&amp;gt;&lt;br /&gt;
* Illustration:&amp;lt;br&amp;gt;&lt;br /&gt;
Adapter pattern illustration-migration of Mac OS X from a 1998 PowerPC-based iMac to a 2007 Intel-based iMac. This section shows how the adapter pattern has been used to enable Mac OS migration.&amp;lt;br&amp;gt;&lt;br /&gt;
* Design:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern's important contribution is that it promotes programming to interfaces. The Client works to a domain-specific standard, which is specified in the ITarget interface. An Adaptee class provides the required functionality, but with a different interface. The Adapter implements the ITarget interface and routes calls from the Client through to the Adaptee, making whatever changes to parameters and return types are necessary to meet the requirements. A Target class that implements the ITarget interface directly could exist, but this is not a necessary part of the pattern. In any case, the Client is aware only of the ITarget interface, and it relies on that for its correct operation.&amp;lt;br&amp;gt;&lt;br /&gt;
* Implementation:&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides an example in C# and how the Adapter, Adaptee, Target and Client all communicate with each other.&lt;br /&gt;
* Two way adapters and Pluggable adapters:&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describes the other kinds of adapter with appropriate examples.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This page gives an in-depth explanation of the adapter pattern. It shows how the adapter pattern is implemented in C#. The real world examples makes it easy to grasp the topic.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; The page might be too long for a quick review. Though it is really good for a beginner, an Intermediate or an expert person in design pattern may not actually want to wade through the entire concept in such a depth. ( unless the person doesn't have much work to do, &amp;quot;eyebrows raised&amp;quot; ).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.oodesign.com/adapter-pattern.html Adpater Pattern on OODesign.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Motivation for using Adapter pattern, intent behind using the adapter pattern, a UML diagram to explain the adapter pattern ( like we did above, yeah! ) to start with. It also covers the application and implementation of Adapter patterns. Moreover, it provides some really nice examples and specific problems where adapter pattern becomes a good fit to use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;The adapter pattern is adapting between classes and objects. Like any adapter in the real world it is used to be an interface, a bridge between two objects. In real world we have adapters for power supplies, adapters for camera memory cards, and so on.&amp;lt;br&amp;gt;&lt;br /&gt;
What about software development? It's the same. Can you imagine an situation when you have some class expecting some type of object and you have an object offering the same features, but exposing a different interface? Of course, you want to use both of them so you don't to implement again one of them, and you don't want to change existing classes, so why not create an adapter...&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Intent&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Convert the interface of a class into another interface clients expect&lt;br /&gt;
# Adapter lets classes work together, that could not otherwise because of incompatible interfaces&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Explains the implementation of the Adapter pattern with the help of a UML diagram.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Applicability and Examples&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Adapters are encountered everywhere. From real world adapters to software adapters.&lt;br /&gt;
# Non Software Examples of Adapter Patterns: Power Supply Adapters, card readers and adapters, ...&lt;br /&gt;
# Software Examples of Adapter Patterns: Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code. &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Specific problem and Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Objects Adapters - Based on Delegation&amp;lt;br&amp;gt; It uses composition, the Adaptee delegates the calls to Adaptee (opossed to class adapters which extends the Adaptee).&lt;br /&gt;
# Class Adapters - Based on (Multiple) Inheritance&amp;lt;br&amp;gt; Class adapters can be implemented in languages supporting multiple inheritance(Java, C# or PHP does not support multiple inheritance). Thus, such adapters can not be easy implemented in Java, C# or VB.NET. Class adapter uses inheritance instead of composition.&lt;br /&gt;
# Adapter Pattern and Strategy Pattern&amp;lt;br&amp;gt; Cases when Adapter Pattern can be a better option instead of Strategy pattern ( ok, now that sounds something interesting, doesn't it?)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; Helps to learn Adapter pattern in detail. Provides good examples to understand how the pattern works and also give suitable cases where adapter design pattern is most applicable. Good for all readers ( beginners, intermediate and experts )&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; Doesn't provides enough figures to understand the concept. For readers who endorse figures, not a good page to look out for.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.codeproject.com/Articles/42915/Adapter-Design-Pattern Adpater Pattern on Codeproject.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Background, Introduction to adapter pattern and its structure, class diagram and implementation of adapter pattern for a scenario.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;This page uses a running example in the background to discuss the design pattern and then further provides a design pattern implementation for that particular example. Moreover, it explains the importance of Adapter Design in system integration when other components have to be adapted by the existing system.&lt;br /&gt;
* Background:&amp;lt;br&amp;gt;&lt;br /&gt;
The webpage continues using the running example to describe the earlier design patterns and further gives a scenario to introduce the adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Design and implementation&amp;lt;br&amp;gt;&lt;br /&gt;
It gives a class diagram describing the adapter pattern for the example in the background section. Also, this section covers the explanation and implementation of all the classes and interfaces mentioned in the below class diagram:&lt;br /&gt;
[[File:AdapterClassOnCodeProject.JPG|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This website is useful for intermediate users of design patterns. Some preexisting knowledge of adapter pattern prior to reading this page can help to further understand the topic. The example provided gives a clear view of the design pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; A beginner might find not find this page a good base line to start. The code on the page might be difficult for a novice programmer to understand.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://csc.columbusstate.edu/woolbright/java/adapter.html Adpater Pattern on Columbus State University website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Introduction to Adapter pattern, Object-Oriented Adapters, an example which implements the Adapter pattern and real world adapters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* Intrduction:&amp;lt;br&amp;gt; The adapter pattern changes the look of an interface. It takes an interface and adapts it to one the client is expecting.&lt;br /&gt;
   The Adapter Pattern converts the interface of a class into another interface the client expects. &lt;br /&gt;
   Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== [http://sourcemaking.com/design_patterns/adapter Adpater Pattern on Sourcemaking.com] ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Intent of Adapter Pattern, Explanation of the pattern, structure of the pattern, example, rule of thumb for adapter pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. This webpage gives a basic idea of adapter pattern and gives examples of the pattern in various programming languages.&lt;br /&gt;
&lt;br /&gt;
* Intent:&amp;lt;br&amp;gt;&lt;br /&gt;
The need and purpose of adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Problem and Discussion:&amp;lt;br&amp;gt;&lt;br /&gt;
An “off the shelf” component offers compelling functionality that you would like to reuse, but its “view of the world” is not compatible with the philosophy and architecture of the system currently being developed. This section tells the problems and how adapter pattern helps solve them.&amp;lt;br&amp;gt;&lt;br /&gt;
* Structure:&amp;lt;br&amp;gt;&lt;br /&gt;
Class diagrams showing the different structures of adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Example:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients. Socket wrenches provide an example of the Adapter. &amp;lt;br&amp;gt;&lt;br /&gt;
* Check list and Rule of thumb:&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describe when and how adapter pattern is useful. It also describes the various things to keep in mind when working with adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Adapter Code examples:&amp;lt;br&amp;gt;&lt;br /&gt;
Links to examples of Adapter pattern implemented in various languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; A good place to study the adapter pattern from scratch. The examples of code in different languages lets us compare the implementation across all these languages. The links contain easy to understand code making it simple for beginers.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70106</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w47 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70106"/>
		<updated>2012-11-18T06:54:48Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Adapter Pattern, a directory of sites'''=&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Adapter Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Sometimes, objects just don't fit together as they should. A class may have changed, or an object turns out to be just too difficult to work with. At such times, adapter design pattern comes to the rescue. Adapter pattern lets you adapt what an object or class has to offer so that another object or class can make use of it. It also helps when classes can't work together because of incompatible interfaces.&lt;br /&gt;
&lt;br /&gt;
Since we are huge fans of visual approach, lets understand the adapter pattern with the help of a diagram. Hold on! Before going in to the UML diagram ( which is kind of less attractive, right? May be .. ) lets look at the figure below which is more appealing.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternForWiki.jpg|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;The above example helps readers quickly get across the &amp;lt;b&amp;gt;intent&amp;lt;/b&amp;gt; of the pattern than the UML diagram shown below. Looking at the UML diagram now, can help the readers to learn about the precise relationships between the classes in adapter pattern and it makes a lot more sense!&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternUMLForWiki.jpg|thumb|500px|center|UML diagram for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, this is all what we had to talk about Adapter Pattern. Lets look at other websites and see what they say about Adapter pattern, (which is probably, the main purpose of the wiki page).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Directory of sites =&lt;br /&gt;
&lt;br /&gt;
=== [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Pattern on wikipedia] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Lists various structures of Adapter Pattern viz., Object Adapter Pattern, Class Adapter Pattern, Runtime Adapter pattern and implementation of Adapter pattern. It provides a few examples for the above structures.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).&amp;lt;br&amp;gt; The various Adapter pattern structures are as follows&lt;br /&gt;
* Object Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.&amp;lt;br&amp;gt;&lt;br /&gt;
* Class Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing.&amp;lt;br&amp;gt;&lt;br /&gt;
* A further form of runtime Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides a good example for runtime Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Implementation:&amp;lt;br&amp;gt;&lt;br /&gt;
Provides a basic skeleton of how to implement the adapter pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This wikipedia page works if readers want a quick review of the adapter pattern. Also it provides good references to read more about the Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; It doesn't helps to understand the Adapter pattern in depth. The examples provided are not easy to comprehend ( unless you are an experienced developer and not a beginner! ). Its not a good page to refer for the beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://msdn.microsoft.com/en-us/library/orm-9780596527730-01-04.aspx Adpater Pattern on MSDN Microsoft Website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Role of adapter pattern, Illustrations or examples where adapter pattern is used ,Design and Implementation of adapter pattern, few more kinds of adapters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter pattern is a versatile pattern that joins together types that were not designed to work with each other. It is one of those patterns that comes in useful when dealing with legacy code-i.e., code that was written a while ago and to which one might not have access.&lt;br /&gt;
* Role:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern enables a system to use classes whose interfaces don't quite match its requirements. It is especially useful for off-the-shelf code, for toolkits, and for libraries&amp;lt;br&amp;gt;&lt;br /&gt;
* Illustration:&amp;lt;br&amp;gt;&lt;br /&gt;
Adapter pattern illustration-migration of Mac OS X from a 1998 PowerPC-based iMac to a 2007 Intel-based iMac. This section shows how the adapter pattern has been used to enable Mac OS migration.&amp;lt;br&amp;gt;&lt;br /&gt;
* Design:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern's important contribution is that it promotes programming to interfaces. The Client works to a domain-specific standard, which is specified in the ITarget interface. An Adaptee class provides the required functionality, but with a different interface. The Adapter implements the ITarget interface and routes calls from the Client through to the Adaptee, making whatever changes to parameters and return types are necessary to meet the requirements. A Target class that implements the ITarget interface directly could exist, but this is not a necessary part of the pattern. In any case, the Client is aware only of the ITarget interface, and it relies on that for its correct operation.&amp;lt;br&amp;gt;&lt;br /&gt;
* Implementation:&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides an example in C# and how the Adapter, Adaptee, Target and Client all communicate with each other.&lt;br /&gt;
* Two way adapters and Pluggable adapters:&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describes the other kinds of adapter with appropriate examples.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This page gives an in-depth explanation of the adapter pattern. It shows how the adapter pattern is implemented in C#. The real world examples makes it easy to grasp the topic.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; The page might be too long for a quick review. Though it is really good for a beginner, an Intermediate or an expert person in design pattern may not actually want to wade through the entire concept in such a depth. ( unless the person doesn't have much work to do, &amp;quot;eyebrows raised&amp;quot; ).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.oodesign.com/adapter-pattern.html Adpater Pattern on OODesign.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Motivation for using Adapter pattern, intent behind using the adapter pattern, a UML diagram to explain the adapter pattern ( like we did above, yeah! ) to start with. It also covers the application and implementation of Adapter patterns. Moreover, it provides some really nice examples and specific problems where adapter pattern becomes a good fit to use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;The adapter pattern is adapting between classes and objects. Like any adapter in the real world it is used to be an interface, a bridge between two objects. In real world we have adapters for power supplies, adapters for camera memory cards, and so on.&amp;lt;br&amp;gt;&lt;br /&gt;
What about software development? It's the same. Can you imagine an situation when you have some class expecting some type of object and you have an object offering the same features, but exposing a different interface? Of course, you want to use both of them so you don't to implement again one of them, and you don't want to change existing classes, so why not create an adapter...&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Intent&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Convert the interface of a class into another interface clients expect&lt;br /&gt;
# Adapter lets classes work together, that could not otherwise because of incompatible interfaces&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Explains the implementation of the Adapter pattern with the help of a UML diagram.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Applicability and Examples&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Adapters are encountered everywhere. From real world adapters to software adapters.&lt;br /&gt;
# Non Software Examples of Adapter Patterns: Power Supply Adapters, card readers and adapters, ...&lt;br /&gt;
# Software Examples of Adapter Patterns: Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code. &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Specific problem and Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Objects Adapters - Based on Delegation&amp;lt;br&amp;gt; It uses composition, the Adaptee delegates the calls to Adaptee (opossed to class adapters which extends the Adaptee).&lt;br /&gt;
# Class Adapters - Based on (Multiple) Inheritance&amp;lt;br&amp;gt; Class adapters can be implemented in languages supporting multiple inheritance(Java, C# or PHP does not support multiple inheritance). Thus, such adapters can not be easy implemented in Java, C# or VB.NET. Class adapter uses inheritance instead of composition.&lt;br /&gt;
# Adapter Pattern and Strategy Pattern&amp;lt;br&amp;gt; Cases when Adapter Pattern can be a better option instead of Strategy pattern ( ok, now that sounds something interesting, doesn't it?)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; Helps to learn Adapter pattern in detail. Provides good examples to understand how the pattern works and also give suitable cases where adapter design pattern is most applicable. Good for all readers ( beginners, intermediate and experts )&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; Doesn't provides enough figures to understand the concept. For readers who endorse figures, not a good page to look out for.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== [http://www.codeproject.com/Articles/42915/Adapter-Design-Pattern Adpater Pattern on Codeproject.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Background, Introduction to adapter pattern and its structure, class diagram and implementation of adapter pattern for an example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;This page discusses an example in the background and then further uses that example to explain the implementation of the adapter pattern. Adapter Design is very useful for the system integration when some other components have to be adapted by the existing system.&lt;br /&gt;
* Background:&amp;lt;br&amp;gt;&lt;br /&gt;
The webpage continues an example it used to describe the earlier design patterns and further gives a scenario to introduce the adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Design and implementation&amp;lt;br&amp;gt;&lt;br /&gt;
They give a class diagram describing the adapter pattern in the example in the background section. Also the implementation and explanation of all the classes and interfaces in the class diagram is given&lt;br /&gt;
[[File:AdapterClassOnCodeProject.JPG|thumb|600px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This website is useful for intermediate users of design patterns. A small understand of adapter pattern prior to reading this page helps further improve the understanding of the topic. The example provided gives a clear view of the design pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; A beginner might find not find this page a good base line to start. The code on the page might be difficult for a novice programmer to understand. More number of examples would have helped further improve the page.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70105</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w47 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70105"/>
		<updated>2012-11-18T06:54:01Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Adapter Pattern, a directory of sites'''=&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Adapter Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Sometimes, objects just don't fit together as they should. A class may have changed, or an object turns out to be just too difficult to work with. At such times, adapter design pattern comes to the rescue. Adapter pattern lets you adapt what an object or class has to offer so that another object or class can make use of it. It also helps when classes can't work together because of incompatible interfaces.&lt;br /&gt;
&lt;br /&gt;
Since we are huge fans of visual approach, lets understand the adapter pattern with the help of a diagram. Hold on! Before going in to the UML diagram ( which is kind of less attractive, right? May be .. ) lets look at the figure below which is more appealing.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternForWiki.jpg|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;The above example helps readers quickly get across the &amp;lt;b&amp;gt;intent&amp;lt;/b&amp;gt; of the pattern than the UML diagram shown below. Looking at the UML diagram now, can help the readers to learn about the precise relationships between the classes in adapter pattern and it makes a lot more sense!&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternUMLForWiki.jpg|thumb|500px|center|UML diagram for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, this is all what we had to talk about Adapter Pattern. Lets look at other websites and see what they say about Adapter pattern, (which is probably, the main purpose of the wiki page).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Directory of sites =&lt;br /&gt;
&lt;br /&gt;
=== [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Pattern on wikipedia] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Lists various structures of Adapter Pattern viz., Object Adapter Pattern, Class Adapter Pattern, Runtime Adapter pattern and implementation of Adapter pattern. It provides a few examples for the above structures.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).&amp;lt;br&amp;gt; The various Adapter pattern structures are as follows&lt;br /&gt;
* Object Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.&amp;lt;br&amp;gt;&lt;br /&gt;
* Class Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing.&amp;lt;br&amp;gt;&lt;br /&gt;
* A further form of runtime Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides a good example for runtime Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Implementation:&amp;lt;br&amp;gt;&lt;br /&gt;
Provides a basic skeleton of how to implement the adapter pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This wikipedia page works if readers want a quick review of the adapter pattern. Also it provides good references to read more about the Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; It doesn't helps to understand the Adapter pattern in depth. The examples provided are not easy to comprehend ( unless you are an experienced developer and not a beginner! ). Its not a good page to refer for the beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://msdn.microsoft.com/en-us/library/orm-9780596527730-01-04.aspx Adpater Pattern on MSDN Microsoft Website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Role of adapter pattern, Illustrations or examples where adapter pattern is used ,Design and Implementation of adapter pattern, few more kinds of adapters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter pattern is a versatile pattern that joins together types that were not designed to work with each other. It is one of those patterns that comes in useful when dealing with legacy code-i.e., code that was written a while ago and to which one might not have access.&lt;br /&gt;
* Role:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern enables a system to use classes whose interfaces don't quite match its requirements. It is especially useful for off-the-shelf code, for toolkits, and for libraries&amp;lt;br&amp;gt;&lt;br /&gt;
* Illustration:&amp;lt;br&amp;gt;&lt;br /&gt;
Adapter pattern illustration-migration of Mac OS X from a 1998 PowerPC-based iMac to a 2007 Intel-based iMac. This section shows how the adapter pattern has been used to enable Mac OS migration.&amp;lt;br&amp;gt;&lt;br /&gt;
* Design:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern's important contribution is that it promotes programming to interfaces. The Client works to a domain-specific standard, which is specified in the ITarget interface. An Adaptee class provides the required functionality, but with a different interface. The Adapter implements the ITarget interface and routes calls from the Client through to the Adaptee, making whatever changes to parameters and return types are necessary to meet the requirements. A Target class that implements the ITarget interface directly could exist, but this is not a necessary part of the pattern. In any case, the Client is aware only of the ITarget interface, and it relies on that for its correct operation.&amp;lt;br&amp;gt;&lt;br /&gt;
* Implementation:&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides an example in C# and how the Adapter, Adaptee, Target and Client all communicate with each other.&lt;br /&gt;
* Two way adapters and Pluggable adapters:&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describes the other kinds of adapter with appropriate examples.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This page gives an in-depth explanation of the adapter pattern. It shows how the adapter pattern is implemented in C#. The real world examples makes it easy to grasp the topic.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; The page might be too long for a quick review. Though it is really good for a beginner, an Intermediate or an expert person in design pattern may not actually want to wade through the entire concept in such a depth. ( unless the person doesn't have much work to do, &amp;quot;eyebrows raised&amp;quot; ).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.oodesign.com/adapter-pattern.html Adpater Pattern on OODesign.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Motivation for using Adapter pattern, intent behind using the adapter pattern, a UML diagram to explain the adapter pattern ( like we did above, yeah! ) to start with. It also covers the application and implementation of Adapter patterns. Moreover, it provides some really nice examples and specific problems where adapter pattern becomes a good fit to use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;The adapter pattern is adapting between classes and objects. Like any adapter in the real world it is used to be an interface, a bridge between two objects. In real world we have adapters for power supplies, adapters for camera memory cards, and so on.&amp;lt;br&amp;gt;&lt;br /&gt;
What about software development? It's the same. Can you imagine an situation when you have some class expecting some type of object and you have an object offering the same features, but exposing a different interface? Of course, you want to use both of them so you don't to implement again one of them, and you don't want to change existing classes, so why not create an adapter...&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Intent&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Convert the interface of a class into another interface clients expect&lt;br /&gt;
# Adapter lets classes work together, that could not otherwise because of incompatible interfaces&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Explains the implementation of the Adapter pattern with the help of a UML diagram.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Applicability and Examples&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Adapters are encountered everywhere. From real world adapters to software adapters.&lt;br /&gt;
# Non Software Examples of Adapter Patterns: Power Supply Adapters, card readers and adapters, ...&lt;br /&gt;
# Software Examples of Adapter Patterns: Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code. &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Specific problem and Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Objects Adapters - Based on Delegation&amp;lt;br&amp;gt; It uses composition, the Adaptee delegates the calls to Adaptee (opossed to class adapters which extends the Adaptee).&lt;br /&gt;
# Class Adapters - Based on (Multiple) Inheritance&amp;lt;br&amp;gt; Class adapters can be implemented in languages supporting multiple inheritance(Java, C# or PHP does not support multiple inheritance). Thus, such adapters can not be easy implemented in Java, C# or VB.NET. Class adapter uses inheritance instead of composition.&lt;br /&gt;
# Adapter Pattern and Strategy Pattern&amp;lt;br&amp;gt; Cases when Adapter Pattern can be a better option instead of Strategy pattern ( ok, now that sounds something interesting, doesn't it?)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; Helps to learn Adapter pattern in detail. Provides good examples to understand how the pattern works and also give suitable cases where adapter design pattern is most applicable. Good for all readers ( beginners, intermediate and experts )&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; Doesn't provides enough figures to understand the concept. For readers who endorse figures, not a good page to look out for.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== [http://www.codeproject.com/Articles/42915/Adapter-Design-Pattern Adpater Pattern on Codeproject.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Background, Introduction to adapter pattern and its structure, class diagram and implementation of adapter pattern for an example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;This page discusses an example in the background and then further uses that example to explain the implementation of the adapter pattern. Adapter Design is very useful for the system integration when some other components have to be adapted by the existing system.&lt;br /&gt;
* Background:&amp;lt;br&amp;gt;&lt;br /&gt;
The webpage continues an example it used to describe the earlier design patterns and further gives a scenario to introduce the adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Design and implementation&amp;lt;br&amp;gt;&lt;br /&gt;
They give a class diagram describing the adapter pattern in the example in the background section. Also the implementation and explanation of all the classes and interfaces in the class diagram is given&lt;br /&gt;
[[File:AdapterClassOnCodeProject.JPG|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This website is useful for intermediate users of design patterns. A small understand of adapter pattern prior to reading this page helps further improve the understanding of the topic. The example provided gives a clear view of the design pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; A beginner might find not find this page a good base line to start. The code on the page might be difficult for a novice programmer to understand. More number of examples would have helped further improve the page.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:AdapterClassOnCodeProject.JPG&amp;diff=70104</id>
		<title>File:AdapterClassOnCodeProject.JPG</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:AdapterClassOnCodeProject.JPG&amp;diff=70104"/>
		<updated>2012-11-18T06:53:10Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: Reference :
http://www.codeproject.com/Articles/42915/Adapter-Design-Pattern&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Reference :&lt;br /&gt;
http://www.codeproject.com/Articles/42915/Adapter-Design-Pattern&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70103</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w47 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70103"/>
		<updated>2012-11-18T06:49:02Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Adapter Pattern, a directory of sites'''=&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Adapter Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Sometimes, objects just don't fit together as they should. A class may have changed, or an object turns out to be just too difficult to work with. At such times, adapter design pattern comes to the rescue. Adapter pattern lets you adapt what an object or class has to offer so that another object or class can make use of it. It also helps when classes can't work together because of incompatible interfaces.&lt;br /&gt;
&lt;br /&gt;
Since we are huge fans of visual approach, lets understand the adapter pattern with the help of a diagram. Hold on! Before going in to the UML diagram ( which is kind of less attractive, right? May be .. ) lets look at the figure below which is more appealing.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternForWiki.jpg|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;The above example helps readers quickly get across the &amp;lt;b&amp;gt;intent&amp;lt;/b&amp;gt; of the pattern than the UML diagram shown below. Looking at the UML diagram now, can help the readers to learn about the precise relationships between the classes in adapter pattern and it makes a lot more sense!&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternUMLForWiki.jpg|thumb|500px|center|UML diagram for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, this is all what we had to talk about Adapter Pattern. Lets look at other websites and see what they say about Adapter pattern, (which is probably, the main purpose of the wiki page).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Directory of sites =&lt;br /&gt;
&lt;br /&gt;
=== [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Pattern on wikipedia] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Lists various structures of Adapter Pattern viz., Object Adapter Pattern, Class Adapter Pattern, Runtime Adapter pattern and implementation of Adapter pattern. It provides a few examples for the above structures.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).&amp;lt;br&amp;gt; The various Adapter pattern structures are as follows&lt;br /&gt;
* Object Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.&amp;lt;br&amp;gt;&lt;br /&gt;
* Class Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing.&amp;lt;br&amp;gt;&lt;br /&gt;
* A further form of runtime Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides a good example for runtime Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Implementation:&amp;lt;br&amp;gt;&lt;br /&gt;
Provides a basic skeleton of how to implement the adapter pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This wikipedia page works if readers want a quick review of the adapter pattern. Also it provides good references to read more about the Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; It doesn't helps to understand the Adapter pattern in depth. The examples provided are not easy to comprehend ( unless you are an experienced developer and not a beginner! ). Its not a good page to refer for the beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://msdn.microsoft.com/en-us/library/orm-9780596527730-01-04.aspx Adpater Pattern on MSDN Microsoft Website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Role of adapter pattern, Illustrations or examples where adapter pattern is used ,Design and Implementation of adapter pattern, few more kinds of adapters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter pattern is a versatile pattern that joins together types that were not designed to work with each other. It is one of those patterns that comes in useful when dealing with legacy code-i.e., code that was written a while ago and to which one might not have access.&lt;br /&gt;
* Role:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern enables a system to use classes whose interfaces don't quite match its requirements. It is especially useful for off-the-shelf code, for toolkits, and for libraries&amp;lt;br&amp;gt;&lt;br /&gt;
* Illustration:&amp;lt;br&amp;gt;&lt;br /&gt;
Adapter pattern illustration-migration of Mac OS X from a 1998 PowerPC-based iMac to a 2007 Intel-based iMac. This section shows how the adapter pattern has been used to enable Mac OS migration.&amp;lt;br&amp;gt;&lt;br /&gt;
* Design:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern's important contribution is that it promotes programming to interfaces. The Client works to a domain-specific standard, which is specified in the ITarget interface. An Adaptee class provides the required functionality, but with a different interface. The Adapter implements the ITarget interface and routes calls from the Client through to the Adaptee, making whatever changes to parameters and return types are necessary to meet the requirements. A Target class that implements the ITarget interface directly could exist, but this is not a necessary part of the pattern. In any case, the Client is aware only of the ITarget interface, and it relies on that for its correct operation.&amp;lt;br&amp;gt;&lt;br /&gt;
* Implementation:&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides an example in C# and how the Adapter, Adaptee, Target and Client all communicate with each other.&lt;br /&gt;
* Two way adapters and Pluggable adapters:&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describes the other kinds of adapter with appropriate examples.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This page gives an in-depth explanation of the adapter pattern. It shows how the adapter pattern is implemented in C#. The real world examples makes it easy to grasp the topic.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; The page might be too long for a quick review. Though it is really good for a beginner, an Intermediate or an expert person in design pattern may not actually want to wade through the entire concept in such a depth. ( unless the person doesn't have much work to do, &amp;quot;eyebrows raised&amp;quot; ).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.oodesign.com/adapter-pattern.html Adpater Pattern on OODesign.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Motivation for using Adapter pattern, intent behind using the adapter pattern, a UML diagram to explain the adapter pattern ( like we did above, yeah! ) to start with. It also covers the application and implementation of Adapter patterns. Moreover, it provides some really nice examples and specific problems where adapter pattern becomes a good fit to use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;The adapter pattern is adapting between classes and objects. Like any adapter in the real world it is used to be an interface, a bridge between two objects. In real world we have adapters for power supplies, adapters for camera memory cards, and so on.&amp;lt;br&amp;gt;&lt;br /&gt;
What about software development? It's the same. Can you imagine an situation when you have some class expecting some type of object and you have an object offering the same features, but exposing a different interface? Of course, you want to use both of them so you don't to implement again one of them, and you don't want to change existing classes, so why not create an adapter...&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Intent&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Convert the interface of a class into another interface clients expect&lt;br /&gt;
# Adapter lets classes work together, that could not otherwise because of incompatible interfaces&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Explains the implementation of the Adapter pattern with the help of a UML diagram.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Applicability and Examples&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Adapters are encountered everywhere. From real world adapters to software adapters.&lt;br /&gt;
# Non Software Examples of Adapter Patterns: Power Supply Adapters, card readers and adapters, ...&lt;br /&gt;
# Software Examples of Adapter Patterns: Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code. &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Specific problem and Implementation&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Objects Adapters - Based on Delegation&amp;lt;br&amp;gt; It uses composition, the Adaptee delegates the calls to Adaptee (opossed to class adapters which extends the Adaptee).&lt;br /&gt;
# Class Adapters - Based on (Multiple) Inheritance&amp;lt;br&amp;gt; Class adapters can be implemented in languages supporting multiple inheritance(Java, C# or PHP does not support multiple inheritance). Thus, such adapters can not be easy implemented in Java, C# or VB.NET. Class adapter uses inheritance instead of composition.&lt;br /&gt;
# Adapter Pattern and Strategy Pattern&amp;lt;br&amp;gt; Cases when Adapter Pattern can be a better option instead of Strategy pattern ( ok, now that sounds something interesting, doesn't it?)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; Helps to learn Adapter pattern in detail. Provides good examples to understand how the pattern works and also give suitable cases where adapter design pattern is most applicable. Good for all readers ( beginners, intermediate and experts )&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; Doesn't provides enough figures to understand the concept. For readers who endorse figures, not a good page to look out for.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== [http://www.codeproject.com/Articles/42915/Adapter-Design-Pattern Adpater Pattern on Codeproject.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Background, Introduction to adapter pattern and its structure, class diagram and implementation of adapter pattern for an example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;This page discusses an example in the background and then further uses that example to explain the implementation of the adapter pattern. Adapter Design is very useful for the system integration when some other components have to be adapted by the existing system.&lt;br /&gt;
* Background:&amp;lt;br&amp;gt;&lt;br /&gt;
The webpage continues an example it used to describe the earlier design patterns and further gives a scenario to introduce the adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Design and implementation&amp;lt;br&amp;gt;&lt;br /&gt;
They give a class diagram describing the adapter pattern in the example in the background section. Also the implementation and explanation of all the classes and interfaces in the class diagram is given&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This website is useful for intermediate users of design patterns. A small understand of adapter pattern prior to reading this page helps further improve the understanding of the topic. The example provided gives a clear view of the design pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; A beginner might find not find this page a good base line to start. The code on the page might be difficult for a novice programmer to understand. More number of examples would have helped further improve the page.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70091</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w47 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w47_am&amp;diff=70091"/>
		<updated>2012-11-18T06:11:16Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Directory of sites */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Adapter Pattern, a directory of sites'''=&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Adapter Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Sometimes, objects just don't fit together as they should. A class may have changed, or an object turns out to be just too difficult to work with. At such times, adapter design pattern comes to the rescue. Adapter pattern lets you adapt what an object or class has to offer so that another object or class can make use of it. It also helps when classes can't work together because of incompatible interfaces.&lt;br /&gt;
&lt;br /&gt;
Since we are huge fans of visual approach, lets understand the adapter pattern with the help of a diagram. Hold on! Before going in to the UML diagram ( which is kind of less attractive, right? May be .. ) lets look at the figure below which is more appealing.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternForWiki.jpg|thumb|500px|center|A real world example for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;The above example helps readers quickly get across the &amp;lt;b&amp;gt;intent&amp;lt;/b&amp;gt; of the pattern than the UML diagram shown below. Looking at the UML diagram now, can help the readers to learn about the precise relationships between the classes in adapter pattern and it makes a lot more sense!&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:AdapterPatternUMLForWiki.jpg|thumb|500px|center|UML diagram for the Adapter Pattern.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, this is all what we had to talk about Adapter Pattern. Lets look at other websites and see what they say about Adapter pattern, (which is probably, the main purpose of the wiki page).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Directory of sites =&lt;br /&gt;
&lt;br /&gt;
=== [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Pattern on wikipedia] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Lists various structures of Adapter Pattern viz., Object Adapter Pattern, Class Adapter Pattern, Runtime Adapter pattern and implementation of Adapter pattern. It provides a few examples for the above structures.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).&amp;lt;br&amp;gt; The various Adapter pattern structures are as follows&lt;br /&gt;
* Object Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.&amp;lt;br&amp;gt;&lt;br /&gt;
* Class Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing.&amp;lt;br&amp;gt;&lt;br /&gt;
* A further form of runtime Adapter pattern:&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides a good example for runtime Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
* Implementation:&amp;lt;br&amp;gt;&lt;br /&gt;
Provides a basic skeleton of how to implement the adapter pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This wikipedia page works if readers want a quick review of the adapter pattern. Also it provides good references to read more about the Adapter pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; It doesn't helps to understand the Adapter pattern in depth. The examples provided are not easy to comprehend ( unless you are an experienced developer and not a beginner! ). Its not a good page to refer for the beginners.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://msdn.microsoft.com/en-us/library/orm-9780596527730-01-04.aspx Adpater Pattern on MSDN Microsoft Website] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Role of adapter pattern, Illustrations or examples where adapter pattern is used ,Design and Implementation of adapter pattern, few more kinds of adapters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;Adapter pattern is a versatile pattern that joins together types that were not designed to work with each other. It is one of those patterns that comes in useful when dealing with legacy code-i.e., code that was written a while ago and to which one might not have access.&lt;br /&gt;
* Role:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern enables a system to use classes whose interfaces don't quite match its requirements. It is especially useful for off-the-shelf code, for toolkits, and for libraries&amp;lt;br&amp;gt;&lt;br /&gt;
* Illustration:&amp;lt;br&amp;gt;&lt;br /&gt;
Adapter pattern illustration-migration of Mac OS X from a 1998 PowerPC-based iMac to a 2007 Intel-based iMac. This section shows how the adapter pattern has been used to enable Mac OS migration.&amp;lt;br&amp;gt;&lt;br /&gt;
* Design:&amp;lt;br&amp;gt;&lt;br /&gt;
The Adapter pattern's important contribution is that it promotes programming to interfaces. The Client works to a domain-specific standard, which is specified in the ITarget interface. An Adaptee class provides the required functionality, but with a different interface. The Adapter implements the ITarget interface and routes calls from the Client through to the Adaptee, making whatever changes to parameters and return types are necessary to meet the requirements. A Target class that implements the ITarget interface directly could exist, but this is not a necessary part of the pattern. In any case, the Client is aware only of the ITarget interface, and it relies on that for its correct operation.&amp;lt;br&amp;gt;&lt;br /&gt;
* Implementation:&amp;lt;br&amp;gt;&lt;br /&gt;
This section provides an example in C# and how the Adapter, Adaptee, Target and Client all communicate with each other.&lt;br /&gt;
* Two way adapters and Pluggable adapters:&amp;lt;br&amp;gt;&lt;br /&gt;
These sections describes the other kinds of adapter with appropriate examples.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Pros:&amp;lt;/b&amp;gt; This page gives an in-depth explanation of the adapter pattern. It shows how the adapter pattern is implemented in C#. The real world examples makes it easy to grasp the topic.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Cons:&amp;lt;/b&amp;gt; The page might be too long for a quick review. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [http://www.oodesign.com/adapter-pattern.html Adpater Pattern on OODesign.com] ===&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Topics Covered:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; Motivation for using Adapter pattern, intent behind using the adapter pattern, a UML diagram to explain the adapter pattern ( like we did above, yeah! ) to start with. It also covers the application and implementation of Adapter patterns. Moreover, it provides some really nice examples and specific problems where adapter pattern becomes a good fit to use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Summary:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;The adapter pattern is adapting between classes and objects. Like any adapter in the real world it is used to be an interface, a bridge between two objects. In real world we have adapters for power supplies, adapters for camera memory cards, and so on.&amp;lt;br&amp;gt;&lt;br /&gt;
What about software development? It's the same. Can you imagine an situation when you have some class expecting some type of object and you have an object offering the same features, but exposing a different interface? Of course, you want to use both of them so you don't to implement again one of them, and you don't want to change existing classes, so why not create an adapter...&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67450</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67450"/>
		<updated>2012-10-10T23:30:55Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use  Don't Repeat Yourself [http://en.wikipedia.org/wiki/Don%27t_repeat_yourself (DRY)] principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are [http://ruby.bastardsbook.com/chapters/collections/ &amp;lt;b&amp;gt;Collection&amp;lt;/b&amp;gt;] defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&amp;lt;br&amp;gt;&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms means constructs in ruby that mimics [http://en.wikipedia.org/wiki/Functional_programming Functional Programming Language].&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed &amp;lt;i&amp;gt;crazy&amp;lt;/i&amp;gt;, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Functional Idioms =&lt;br /&gt;
&lt;br /&gt;
Functional idioms refers to the various constructs in ruby that mimics Functional Programming Language. Most programmers argue that Ruby is actually an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming Language] and it does not behaves as per the functional programming paradigm. However, though the argument is open ended, Ruby is a [http://en.wikipedia.org/wiki/Programming_paradigm#Multi-paradigm_programming_language multi-paradigm language] that supports a functional style of programming, ( supporting functional style of programming doesn't make the language a Functional Programming language ). Ruby does supports the functional programming behavior of avoiding mutable states [http://en.wikipedia.org/wiki/Immutable_object (Immutable and Mutable Object)]. To make the concept clear, consider the example shown below which uses the iterator &amp;quot;&amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;quot;. This type of iteration in ruby is actually a function because ruby passes a function ( block of code ) to be called on each item in the collection&amp;lt;ref&amp;gt;&amp;quot;Ruby a functional language&amp;quot; http://stackoverflow.com/questions/159797/is-ruby-a-functional-language&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a Pythagorean triple!&amp;quot;&lt;br /&gt;
    [3, 4, 5].each { |num| print num }&lt;br /&gt;
&lt;br /&gt;
In the above example&amp;lt;ref&amp;gt;&amp;quot;Functional programming in ruby&amp;quot; http://tech.rufy.com/2006/11/functional-programming-in-ruby.html&amp;lt;/ref&amp;gt;, the function being passed is { |num| puts num }. In fact, the code shown below accomplishes the same behavior as done above:&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a Pythagorean triple!&amp;quot;&lt;br /&gt;
    my_print_function = lambda { |num| print num }&lt;br /&gt;
    [3, 4, 5].each &amp;amp;my_print_function&lt;br /&gt;
&lt;br /&gt;
Here, in the example shown above, we use the address of &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot; to identify the lines of code that needs to be executed and for each element of the collection execute the function &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot;. So, in that respect, we can call the iterator &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; a functional idiom. &lt;br /&gt;
&lt;br /&gt;
Now, this is very much from the functional programming paradigm, right? ( may be or may be not, lets keep this job for the debaters! ) Readers are encouraged to please refer [http://khelll.com/blog/ruby/ruby-and-functional-programming here] for further understanding of functional idioms.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For curious readers, both the above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    This is a Pythagorean triple!&lt;br /&gt;
    [3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Ruby provides such a flexible approach to use iterators as compared to C++ and Java that it is worth learning these iterators and to try out them while writing code ( or just for fun if programmers want to!). In ruby the iterator is simply a method that calls yield and passes control to a block of code associated with this iterator method. Unlike Java and C++, ruby does not requires the need to generate helper classes to carry the iterator state thus making it a transparent language. A beginner in ruby may think that the working of a block and yield is magical but actually, it is not! ( the wiki page just explains this magical behavior, right? ). As programmers keeps working on iterators, blocks and functional idioms they understand how easy it is to define your behavior without having the overhead of extra utilities. And this makes ruby programming so easy because you concentrate on getting the job done, not on building [http://en.wikipedia.org/wiki/Scaffold_%28programming%29 scaffolding] to support the language itself.&amp;lt;ref&amp;gt;&amp;quot;Ruby Blocks, Iterators and containers&amp;quot; http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;br /&gt;
&lt;br /&gt;
[http://khelll.com/blog/ruby/ruby-and-functional-programming/ Functional Idioms] : Functional idioms explained.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67449</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67449"/>
		<updated>2012-10-10T23:29:47Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Functional Idioms */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use  Don't Repeat Yourself [http://en.wikipedia.org/wiki/Don%27t_repeat_yourself (DRY)] principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are [http://ruby.bastardsbook.com/chapters/collections/ &amp;lt;b&amp;gt;Collection&amp;lt;/b&amp;gt;] defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&amp;lt;br&amp;gt;&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms means constructs in ruby that mimics [http://en.wikipedia.org/wiki/Functional_programming Functional Programming Language].&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed &amp;lt;i&amp;gt;crazy&amp;lt;/i&amp;gt;, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Functional Idioms =&lt;br /&gt;
&lt;br /&gt;
Functional idioms refers to the various constructs in ruby that mimics Functional Programming Language. Most programmers argue that Ruby is actually an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming Language] and it does not behaves as per the functional programming paradigm. However, though the argument is open ended, Ruby is a [http://en.wikipedia.org/wiki/Programming_paradigm#Multi-paradigm_programming_language multi-paradigm language] that supports a functional style of programming, ( supporting functional style of programming doesn't make the language a Functional Programming language ). Ruby does supports the functional programming behavior of avoiding mutable states [http://en.wikipedia.org/wiki/Immutable_object (Immutable and Mutable Object)]. To make the concept clear, consider the example shown below which uses the iterator &amp;quot;&amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;quot;. This type of iteration in ruby is actually a function because ruby passes a function ( block of code ) to be called on each item in the collection&amp;lt;ref&amp;gt;&amp;quot;Ruby a functional language&amp;quot; http://stackoverflow.com/questions/159797/is-ruby-a-functional-language&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a Pythagorean triple!&amp;quot;&lt;br /&gt;
    [3, 4, 5].each { |num| print num }&lt;br /&gt;
&lt;br /&gt;
In the above example&amp;lt;ref&amp;gt;&amp;quot;Functional programming in ruby&amp;quot; http://tech.rufy.com/2006/11/functional-programming-in-ruby.html&amp;lt;/ref&amp;gt;, the function being passed is { |num| puts num }. In fact, the code shown below accomplishes the same behavior as done above:&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a Pythagorean triple!&amp;quot;&lt;br /&gt;
    my_print_function = lambda { |num| print num }&lt;br /&gt;
    [3, 4, 5].each &amp;amp;my_print_function&lt;br /&gt;
&lt;br /&gt;
Here, in the example shown above, we use the address of &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot; to identify the lines of code that needs to be executed and for each element of the collection execute the function &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot;. So, in that respect, we can call the iterator &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; a functional idiom. &lt;br /&gt;
&lt;br /&gt;
Now, this is very much from the functional programming paradigm, right? ( may be or may be not, lets keep this job for the debaters! ) Readers are encouraged to please refer [http://khelll.com/blog/ruby/ruby-and-functional-programming here] for further understanding of functional idioms.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For curious readers, both the above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    This is a Pythagorean triple!&lt;br /&gt;
    [3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Ruby provides such a flexible approach to use iterators as compared to C++ and Java that it is worth learning these iterators and to try out them while writing code ( or just for fun if programmers want to!). In ruby the iterator is simply a method that calls yield and passes control to a block of code associated with this iterator method. Unlike Java and C++, ruby does not requires the need to generate helper classes to carry the iterator state thus making it a transparent language. A beginner in ruby may think that the working of a block and yield is magical but actually, it is not! ( the wiki page just explains this magical behavior, right? ). As programmers keeps working on iterators, blocks and functional idioms they understand how easy it is to define your behavior without having the overhead of extra utilities. And this makes ruby programming so easy because you concentrate on getting the job done, not on building scaffolding to support the language itself.&amp;lt;ref&amp;gt;&amp;quot;Ruby Blocks, Iterators and containers&amp;quot; http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;br /&gt;
&lt;br /&gt;
[http://khelll.com/blog/ruby/ruby-and-functional-programming/ Functional Idioms] : Functional idioms explained.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67448</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67448"/>
		<updated>2012-10-10T23:25:03Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use  Don't Repeat Yourself [http://en.wikipedia.org/wiki/Don%27t_repeat_yourself (DRY)] principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are [http://ruby.bastardsbook.com/chapters/collections/ &amp;lt;b&amp;gt;Collection&amp;lt;/b&amp;gt;] defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&amp;lt;br&amp;gt;&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms means constructs in ruby that mimics [http://en.wikipedia.org/wiki/Functional_programming Functional Programming Language].&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed &amp;lt;i&amp;gt;crazy&amp;lt;/i&amp;gt;, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Functional Idioms =&lt;br /&gt;
&lt;br /&gt;
Functional idioms refers to the various constructs in ruby that mimics Functional Programming Language. Most programmers argue that Ruby is actually an Object Oriented Programming Language and it does not behaves as per the functional programming paradigm. However, though the argument is open ended, Ruby is a multi-paradigm language that supports a functional style of programming, ( supporting functional style of programming doesn't make the language a Functional Programming language ). Ruby does supports the functional programming behavior of avoiding mutable states. To make the concept clear, consider the example shown below which uses the iterator &amp;quot;&amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;quot;. This type of iteration in ruby is actually a function because ruby passes a function ( block of code ) to be called on each item in the collection&amp;lt;ref&amp;gt;&amp;quot;Ruby a functional language&amp;quot; http://stackoverflow.com/questions/159797/is-ruby-a-functional-language&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a Pythagorean triple!&amp;quot;&lt;br /&gt;
    [3, 4, 5].each { |num| print num }&lt;br /&gt;
&lt;br /&gt;
In the above example&amp;lt;ref&amp;gt;&amp;quot;Functional programming in ruby&amp;quot; http://tech.rufy.com/2006/11/functional-programming-in-ruby.html&amp;lt;/ref&amp;gt;, the function being passed is { |num| puts num }. In fact, the code shown below accomplishes the same behavior as done above:&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a Pythagorean triple!&amp;quot;&lt;br /&gt;
    my_print_function = lambda { |num| print num }&lt;br /&gt;
    [3, 4, 5].each &amp;amp;my_print_function&lt;br /&gt;
&lt;br /&gt;
Here, in the example shown above, we use the address of &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot; to identify the lines of code that needs to be executed and for each element of the collection execute the function &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot;. So, in that respect, we can call the iterator &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; a functional idiom. &lt;br /&gt;
&lt;br /&gt;
Now, this is very much from the functional programming paradigm, right? ( may be or may be not, lets keep this job for the debaters! ) Readers are encouraged to please refer [http://khelll.com/blog/ruby/ruby-and-functional-programming here] for further understanding of functional idioms.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For curious readers, both the above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    This is a Pythagorean triple!&lt;br /&gt;
    [3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Ruby provides such a flexible approach to use iterators as compared to C++ and Java that it is worth learning these iterators and to try out them while writing code ( or just for fun if programmers want to!). In ruby the iterator is simply a method that calls yield and passes control to a block of code associated with this iterator method. Unlike Java and C++, ruby does not requires the need to generate helper classes to carry the iterator state thus making it a transparent language. A beginner in ruby may think that the working of a block and yield is magical but actually, it is not! ( the wiki page just explains this magical behavior, right? ). As programmers keeps working on iterators, blocks and functional idioms they understand how easy it is to define your behavior without having the overhead of extra utilities. And this makes ruby programming so easy because you concentrate on getting the job done, not on building scaffolding to support the language itself.&amp;lt;ref&amp;gt;&amp;quot;Ruby Blocks, Iterators and containers&amp;quot; http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;br /&gt;
&lt;br /&gt;
[http://khelll.com/blog/ruby/ruby-and-functional-programming/ Functional Idioms] : Functional idioms explained.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67447</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67447"/>
		<updated>2012-10-10T23:22:32Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use  Don't Repeat Yourself [http://en.wikipedia.org/wiki/Don%27t_repeat_yourself (DRY)] principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are [http://ruby.bastardsbook.com/chapters/collections/ &amp;lt;b&amp;gt;Collection&amp;lt;/b&amp;gt;] defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&amp;lt;br&amp;gt;&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms means constructs in ruby that mimics Functional Programming Language.&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed &amp;lt;i&amp;gt;crazy&amp;lt;/i&amp;gt;, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Functional Idioms =&lt;br /&gt;
&lt;br /&gt;
Functional idioms refers to the various constructs in ruby that mimics Functional Programming Language. Most programmers argue that Ruby is actually an Object Oriented Programming Language and it does not behaves as per the functional programming paradigm. However, though the argument is open ended, Ruby is a multi-paradigm language that supports a functional style of programming, ( supporting functional style of programming doesn't make the language a Functional Programming language ). Ruby does supports the functional programming behavior of avoiding mutable states. To make the concept clear, consider the example shown below which uses the iterator &amp;quot;&amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;quot;. This type of iteration in ruby is actually a function because ruby passes a function ( block of code ) to be called on each item in the collection&amp;lt;ref&amp;gt;&amp;quot;Ruby a functional language&amp;quot; http://stackoverflow.com/questions/159797/is-ruby-a-functional-language&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a Pythagorean triple!&amp;quot;&lt;br /&gt;
    [3, 4, 5].each { |num| print num }&lt;br /&gt;
&lt;br /&gt;
In the above example&amp;lt;ref&amp;gt;&amp;quot;Functional programming in ruby&amp;quot; http://tech.rufy.com/2006/11/functional-programming-in-ruby.html&amp;lt;/ref&amp;gt;, the function being passed is { |num| puts num }. In fact, the code shown below accomplishes the same behavior as done above:&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a Pythagorean triple!&amp;quot;&lt;br /&gt;
    my_print_function = lambda { |num| print num }&lt;br /&gt;
    [3, 4, 5].each &amp;amp;my_print_function&lt;br /&gt;
&lt;br /&gt;
Here, in the example shown above, we use the address of &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot; to identify the lines of code that needs to be executed and for each element of the collection execute the function &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot;. So, in that respect, we can call the iterator &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; a functional idiom. &lt;br /&gt;
&lt;br /&gt;
Now, this is very much from the functional programming paradigm, right? ( may be or may be not, lets keep this job for the debaters! ) Readers are encouraged to please refer [http://khelll.com/blog/ruby/ruby-and-functional-programming here] for further understanding of functional idioms.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For curious readers, both the above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    This is a Pythagorean triple!&lt;br /&gt;
    [3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Ruby provides such a flexible approach to use iterators as compared to C++ and Java that it is worth learning these iterators and to try out them while writing code ( or just for fun if programmers want to!). In ruby the iterator is simply a method that calls yield and passes control to a block of code associated with this iterator method. Unlike Java and C++, ruby does not requires the need to generate helper classes to carry the iterator state thus making it a transparent language. A beginner in ruby may think that the working of a block and yield is magical but actually, it is not! ( the wiki page just explains this magical behavior, right? ). As programmers keeps working on iterators, blocks and functional idioms they understand how easy it is to define your behavior without having the overhead of extra utilities. And this makes ruby programming so easy because you concentrate on getting the job done, not on building scaffolding to support the language itself.&amp;lt;ref&amp;gt;&amp;quot;Ruby Blocks, Iterators and containers&amp;quot; http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;br /&gt;
&lt;br /&gt;
[http://khelll.com/blog/ruby/ruby-and-functional-programming/ Functional Idioms] : Functional idioms explained.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67446</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67446"/>
		<updated>2012-10-10T23:21:23Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use  Don't Repeat Yourself [http://en.wikipedia.org/wiki/Don%27t_repeat_yourself (DRY)] principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are &amp;lt;b&amp;gt;Collection&amp;lt;/b&amp;gt; defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&amp;lt;br&amp;gt;&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms means constructs in ruby that mimics Functional Programming Language.&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed &amp;lt;i&amp;gt;crazy&amp;lt;/i&amp;gt;, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Functional Idioms =&lt;br /&gt;
&lt;br /&gt;
Functional idioms refers to the various constructs in ruby that mimics Functional Programming Language. Most programmers argue that Ruby is actually an Object Oriented Programming Language and it does not behaves as per the functional programming paradigm. However, though the argument is open ended, Ruby is a multi-paradigm language that supports a functional style of programming, ( supporting functional style of programming doesn't make the language a Functional Programming language ). Ruby does supports the functional programming behavior of avoiding mutable states. To make the concept clear, consider the example shown below which uses the iterator &amp;quot;&amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;quot;. This type of iteration in ruby is actually a function because ruby passes a function ( block of code ) to be called on each item in the collection&amp;lt;ref&amp;gt;&amp;quot;Ruby a functional language&amp;quot; http://stackoverflow.com/questions/159797/is-ruby-a-functional-language&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a Pythagorean triple!&amp;quot;&lt;br /&gt;
    [3, 4, 5].each { |num| print num }&lt;br /&gt;
&lt;br /&gt;
In the above example&amp;lt;ref&amp;gt;&amp;quot;Functional programming in ruby&amp;quot; http://tech.rufy.com/2006/11/functional-programming-in-ruby.html&amp;lt;/ref&amp;gt;, the function being passed is { |num| puts num }. In fact, the code shown below accomplishes the same behavior as done above:&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a Pythagorean triple!&amp;quot;&lt;br /&gt;
    my_print_function = lambda { |num| print num }&lt;br /&gt;
    [3, 4, 5].each &amp;amp;my_print_function&lt;br /&gt;
&lt;br /&gt;
Here, in the example shown above, we use the address of &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot; to identify the lines of code that needs to be executed and for each element of the collection execute the function &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot;. So, in that respect, we can call the iterator &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; a functional idiom. &lt;br /&gt;
&lt;br /&gt;
Now, this is very much from the functional programming paradigm, right? ( may be or may be not, lets keep this job for the debaters! ) Readers are encouraged to please refer [http://khelll.com/blog/ruby/ruby-and-functional-programming here] for further understanding of functional idioms.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For curious readers, both the above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    This is a Pythagorean triple!&lt;br /&gt;
    [3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Ruby provides such a flexible approach to use iterators as compared to C++ and Java that it is worth learning these iterators and to try out them while writing code ( or just for fun if programmers want to!). In ruby the iterator is simply a method that calls yield and passes control to a block of code associated with this iterator method. Unlike Java and C++, ruby does not requires the need to generate helper classes to carry the iterator state thus making it a transparent language. A beginner in ruby may think that the working of a block and yield is magical but actually, it is not! ( the wiki page just explains this magical behavior, right? ). As programmers keeps working on iterators, blocks and functional idioms they understand how easy it is to define your behavior without having the overhead of extra utilities. And this makes ruby programming so easy because you concentrate on getting the job done, not on building scaffolding to support the language itself.&amp;lt;ref&amp;gt;&amp;quot;Ruby Blocks, Iterators and containers&amp;quot; http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;br /&gt;
&lt;br /&gt;
[http://khelll.com/blog/ruby/ruby-and-functional-programming/ Functional Idioms] : Functional idioms explained.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67445</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67445"/>
		<updated>2012-10-10T23:20:19Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use  Don't Repeat Yourself (DRY) principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are &amp;lt;b&amp;gt;Collection&amp;lt;/b&amp;gt; defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&amp;lt;br&amp;gt;&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms means constructs in ruby that mimics Functional Programming Language.&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed &amp;lt;i&amp;gt;crazy&amp;lt;/i&amp;gt;, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Functional Idioms =&lt;br /&gt;
&lt;br /&gt;
Functional idioms refers to the various constructs in ruby that mimics Functional Programming Language. Most programmers argue that Ruby is actually an Object Oriented Programming Language and it does not behaves as per the functional programming paradigm. However, though the argument is open ended, Ruby is a multi-paradigm language that supports a functional style of programming, ( supporting functional style of programming doesn't make the language a Functional Programming language ). Ruby does supports the functional programming behavior of avoiding mutable states. To make the concept clear, consider the example shown below which uses the iterator &amp;quot;&amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;quot;. This type of iteration in ruby is actually a function because ruby passes a function ( block of code ) to be called on each item in the collection&amp;lt;ref&amp;gt;&amp;quot;Ruby a functional language&amp;quot; http://stackoverflow.com/questions/159797/is-ruby-a-functional-language&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a Pythagorean triple!&amp;quot;&lt;br /&gt;
    [3, 4, 5].each { |num| print num }&lt;br /&gt;
&lt;br /&gt;
In the above example&amp;lt;ref&amp;gt;&amp;quot;Functional programming in ruby&amp;quot; http://tech.rufy.com/2006/11/functional-programming-in-ruby.html&amp;lt;/ref&amp;gt;, the function being passed is { |num| puts num }. In fact, the code shown below accomplishes the same behavior as done above:&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a Pythagorean triple!&amp;quot;&lt;br /&gt;
    my_print_function = lambda { |num| print num }&lt;br /&gt;
    [3, 4, 5].each &amp;amp;my_print_function&lt;br /&gt;
&lt;br /&gt;
Here, in the example shown above, we use the address of &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot; to identify the lines of code that needs to be executed and for each element of the collection execute the function &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot;. So, in that respect, we can call the iterator &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; a functional idiom. &lt;br /&gt;
&lt;br /&gt;
Now, this is very much from the functional programming paradigm, right? ( may be or may be not, lets keep this job for the debaters! ) Readers are encouraged to please refer [http://khelll.com/blog/ruby/ruby-and-functional-programming here] for further understanding of functional idioms.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For curious readers, both the above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    This is a Pythagorean triple!&lt;br /&gt;
    [3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Ruby provides such a flexible approach to use iterators as compared to C++ and Java that it is worth learning these iterators and to try out them while writing code ( or just for fun if programmers want to!). In ruby the iterator is simply a method that calls yield and passes control to a block of code associated with this iterator method. Unlike Java and C++, ruby does not requires the need to generate helper classes to carry the iterator state thus making it a transparent language. A beginner in ruby may think that the working of a block and yield is magical but actually, it is not! ( the wiki page just explains this magical behavior, right? ). As programmers keeps working on iterators, blocks and functional idioms they understand how easy it is to define your behavior without having the overhead of extra utilities. And this makes ruby programming so easy because you concentrate on getting the job done, not on building scaffolding to support the language itself.&amp;lt;ref&amp;gt;&amp;quot;Ruby Blocks, Iterators and containers&amp;quot; http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;br /&gt;
&lt;br /&gt;
[http://khelll.com/blog/ruby/ruby-and-functional-programming/ Functional Idioms] : Functional idioms explained.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67240</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67240"/>
		<updated>2012-10-04T02:54:55Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Functional Idioms */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed crazy, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Functional Idioms =&lt;br /&gt;
&lt;br /&gt;
Functional idioms refers to the various constructs in ruby that mimics Functional Programming Language. Most programmers argue that Ruby is actually an Object Oriented Programming Language and it does not behaves as per the functional programming paradigm. However, though the arguement is open ended, Ruby is a multi-paradigm language that supports a functional style of programming, (supporting functional style of programming doesn't make the language a Functional Programming language ). Ruby does supports the functional programming behavior of avoiding mutable states. To make the concept clear, consider the example shown below which uses the iterator &amp;quot;&amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;quot;. This type of iteration in ruby is actually a function because ruby passes a function ( block of code ) to be called on each item in the collection&amp;lt;ref&amp;gt;&amp;quot;Ruby a functional language&amp;quot; http://stackoverflow.com/questions/159797/is-ruby-a-functional-language&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a pythagorean triple!&amp;quot;&lt;br /&gt;
    [3, 4, 5].each { |num| print num }&lt;br /&gt;
&lt;br /&gt;
In the above example&amp;lt;ref&amp;gt;&amp;quot;Functional programming in ruby&amp;quot; http://tech.rufy.com/2006/11/functional-programming-in-ruby.html&amp;lt;/ref&amp;gt;, the function being passed is { |num| puts num }. In fact, the code shown below accomplishes the same behavior as done above:&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a pythagorean triple!&amp;quot;&lt;br /&gt;
    my_print_function = lambda { |num| print num }&lt;br /&gt;
    [3, 4, 5].each &amp;amp;my_print_function&lt;br /&gt;
&lt;br /&gt;
Here, in the example shown above, we use the address of &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot; to identify the lines of code that needs to be executed and for each element of the collection execute the function &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot;. So, in that respect, we can call the iterator &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; a functional idiom. &lt;br /&gt;
&lt;br /&gt;
Now, this is very much from the functional programming paradigm, right? ( may be or may be not, lets keep this job for the debaters!). Readers are encouraged to please refer to [http://khelll.com/blog/ruby/ruby-and-functional-programming this] for further understanding of functional idioms.&lt;br /&gt;
&lt;br /&gt;
For curious readers, both the above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    This is a pythagorean triple!&lt;br /&gt;
    [3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Ruby provides such a flexible approach to use iterators as compared to C++ and Java that it is worth learning these iterators and try out them while writing code ( or just for fun if programmers want to!). In ruby the iterator is simply a method that calls yield and passes control to a block of code associated with this iterator method. Unlike Java and C++, ruby does not requires the need to generate helper classes to carry the iterator state thus making it a transparent language. A beginner in ruby may think that the working of a block and yield is magical but actually, it is not! ( the wiki page just explains this magical behavior, right? ). As programmers keeps working on iterators, blocks and functional idioms they understand how easy it is to define your behavior without having the overhead of extra utilities. And this makes ruby programming so easy because you concentrate on getting the job done, not on building scaffolding to support the language itself.&amp;lt;ref&amp;gt;&amp;quot;Ruby Blocks, Iterators and containers&amp;quot; http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;br /&gt;
&lt;br /&gt;
[http://khelll.com/blog/ruby/ruby-and-functional-programming/ Functional Idioms] : Functional idioms explained.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67236</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67236"/>
		<updated>2012-10-04T02:51:18Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Functional Idioms */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed crazy, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Functional Idioms =&lt;br /&gt;
&lt;br /&gt;
Functional idioms refers to the various constructs in ruby that mimics Functional Programming Language. Most programmers argue that Ruby is actually an Object Oriented Programming Language and it does not behaves as per the functional programming paradigm. However, though the arguement is open ended, Ruby is a multi-paradigm language that supports a functional style of programming, (supporting functional style of programming doesn't make the language a Functional Programming language ). Ruby does supports the functional programming behavior of avoiding mutable states. To make the concept clear, consider the example shown below which uses the iterator &amp;quot;&amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;quot;. This type of iteration in ruby is actually a function because ruby passes a function ( block of code ) to be called on each item in the collection&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/159797/is-ruby-a-functional-language&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a pythagorean triple!&amp;quot;&lt;br /&gt;
    [3, 4, 5].each { |num| print num }&lt;br /&gt;
&lt;br /&gt;
In the above example&amp;lt;ref&amp;gt;http://tech.rufy.com/2006/11/functional-programming-in-ruby.html&amp;lt;/ref&amp;gt;, the function being passed is { |num| puts num }. In fact, the code shown below accomplishes the same behavior as done above:&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a pythagorean triple!&amp;quot;&lt;br /&gt;
    my_print_function = lambda { |num| print num }&lt;br /&gt;
    [3, 4, 5].each &amp;amp;my_print_function&lt;br /&gt;
&lt;br /&gt;
Here, in the example shown above, we use the address of &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot; to identify the lines of code that needs to be executed and for each element of the collection execute the function &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot;. So, in that respect, we can call the iterator &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; a functional idiom. &lt;br /&gt;
&lt;br /&gt;
Now, this is very much from the functional programming paradigm, right? ( may be or may be not, lets keep this job for the debaters!). Readers are encouraged to please refer to [http://khelll.com/blog/ruby/ruby-and-functional-programming this] for further understanding of functional idioms.&lt;br /&gt;
&lt;br /&gt;
For curious readers, both the above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    This is a pythagorean triple!&lt;br /&gt;
    [3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Ruby provides such a flexible approach to use iterators as compared to C++ and Java that it is worth learning these iterators and try out them while writing code ( or just for fun if programmers want to!). In ruby the iterator is simply a method that calls yield and passes control to a block of code associated with this iterator method. Unlike Java and C++, ruby does not requires the need to generate helper classes to carry the iterator state thus making it a transparent language. A beginner in ruby may think that the working of a block and yield is magical but actually, it is not! ( the wiki page just explains this magical behavior, right? ). As programmers keeps working on iterators, blocks and functional idioms they understand how easy it is to define your behavior without having the overhead of extra utilities. And this makes ruby programming so easy because you concentrate on getting the job done, not on building scaffolding to support the language itself.&amp;lt;ref&amp;gt;&amp;quot;Ruby Blocks, Iterators and containers&amp;quot; http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;br /&gt;
&lt;br /&gt;
[http://khelll.com/blog/ruby/ruby-and-functional-programming/ Functional Idioms] : Functional idioms explained.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=67220</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=67220"/>
		<updated>2012-10-04T02:41:38Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w37 ss]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w58 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w56 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w55 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w57 mp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w52 an]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch1b 1w38 nm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w60 ac]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w62 rb]]&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67215</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67215"/>
		<updated>2012-10-04T02:39:36Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed crazy, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Functional Idioms =&lt;br /&gt;
&lt;br /&gt;
Functional idioms refers to the various constructs in ruby that mimics Functional Programming Language. Most programmers argue that Ruby is actually an Object Oriented Programming Language and it does not behaves as per the functional programming paradigm. However, thought the argue is open ended, Ruby is a multi-paradigm language that supports a functional style of programming, (supporting functional style of programming doesn't make the language a Functional Programming language ). Ruby does supports the functional programming behavior of avoiding mutable states. To make the concept clear, consider the example shown below which uses the iterator &amp;quot;&amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;quot;. This type of iteration in ruby is actually a function because ruby passes a function ( block of code ) to be called on each item in the collection&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/159797/is-ruby-a-functional-language&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a pythagorean triple!&amp;quot;&lt;br /&gt;
    [3, 4, 5].each { |num| print num }&lt;br /&gt;
&lt;br /&gt;
In the above example&amp;lt;ref&amp;gt;http://tech.rufy.com/2006/11/functional-programming-in-ruby.html&amp;lt;/ref&amp;gt;, the function being passed is { |num| puts num }. In fact, the code shown below accomplishes the same behavior as done above:&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a pythagorean triple!&amp;quot;&lt;br /&gt;
    my_print_function = lambda { |num| print num }&lt;br /&gt;
    [3, 4, 5].each &amp;amp;my_print_function&lt;br /&gt;
&lt;br /&gt;
Here, in the example shown above, we use the address of &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot; to identify the lines of code that needs to be executed and for each element of the collection execute the function &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot;. So, in that respect, we can call the iterator &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; a functional idiom. &lt;br /&gt;
&lt;br /&gt;
Now, this is very much from the functional programming paradigm, right? ( may be or may be not, lets keep this job for the debaters!). Readers are encouraged to please refer to [http://khelll.com/blog/ruby/ruby-and-functional-programming this] for further understanding of functional idioms.&lt;br /&gt;
&lt;br /&gt;
For curious readers, both the above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    This is a pythagorean triple!&lt;br /&gt;
    [3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Ruby provides such a flexible approach to use iterators as compared to C++ and Java that it is worth learning these iterators and try out them while writing code ( or just for fun if programmers want to!). In ruby the iterator is simply a method that calls yield and passes control to a block of code associated with this iterator method. Unlike Java and C++, ruby does not requires the need to generate helper classes to carry the iterator state thus making it a transparent language. A beginner in ruby may think that the working of a block and yield is magical but actually, it is not! ( the wiki page just explains this magical behavior, right? ). As programmers keeps working on iterators, blocks and functional idioms they understand how easy it is to define your behavior without having the overhead of extra utilities. And this makes ruby programming so easy because you concentrate on getting the job done, not on building scaffolding to support the language itself.&amp;lt;ref&amp;gt;&amp;quot;Ruby Blocks, Iterators and containers&amp;quot; http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;br /&gt;
&lt;br /&gt;
[http://khelll.com/blog/ruby/ruby-and-functional-programming/ Functional Idioms] : Functional idioms explained.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67211</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67211"/>
		<updated>2012-10-04T02:38:49Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Functional Idioms */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed crazy, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Functional Idioms =&lt;br /&gt;
&lt;br /&gt;
Functional idioms refers to the various constructs in ruby that mimics Functional Programming Language. Most programmers argue that Ruby is actually an Object Oriented Programming Language and it does not behaves as per the functional programming paradigm. However, thought the argue is open ended, Ruby is a multi-paradigm language that supports a functional style of programming, (supporting functional style of programming doesn't make the language a Functional Programming language ). Ruby does supports the functional programming behavior of avoiding mutable states. To make the concept clear, consider the example shown below which uses the iterator &amp;quot;&amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;quot;. This type of iteration in ruby is actually a function because ruby passes a function ( block of code ) to be called on each item in the collection&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/159797/is-ruby-a-functional-language&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a pythagorean triple!&amp;quot;&lt;br /&gt;
    [3, 4, 5].each { |num| print num }&lt;br /&gt;
&lt;br /&gt;
In the above example&amp;lt;ref&amp;gt;http://tech.rufy.com/2006/11/functional-programming-in-ruby.html&amp;lt;/ref&amp;gt;, the function being passed is { |num| puts num }. In fact, the code shown below accomplishes the same behavior as done above:&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a pythagorean triple!&amp;quot;&lt;br /&gt;
    my_print_function = lambda { |num| print num }&lt;br /&gt;
    [3, 4, 5].each &amp;amp;my_print_function&lt;br /&gt;
&lt;br /&gt;
Here, in the example shown above, we use the address of &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot; to identify the lines of code that needs to be executed and for each element of the collection execute the function &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot;. So, in that respect, we can call the iterator &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; a functional idiom. &lt;br /&gt;
&lt;br /&gt;
Now, this is very much from the functional programming paradigm, right? ( may be or may be not, lets keep this job for the debaters!). Readers are encouraged to please refer to [http://khelll.com/blog/ruby/ruby-and-functional-programming this] for further understanding of functional idioms.&lt;br /&gt;
&lt;br /&gt;
For curious readers, both the above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    This is a pythagorean triple!&lt;br /&gt;
    [3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Ruby provides such a flexible approach to use iterators as compared to C++ and Java that it is worth learning these iterators and try out them while writing code ( or just for fun if programmers want to!). In ruby the iterator is simply a method that calls yield and passes control to a block of code associated with this iterator method. Unlike Java and C++, ruby does not requires the need to generate helper classes to carry the iterator state thus making it a transparent language. A beginner in ruby may think that the working of a block and yield is magical but actually, it is not! ( the wiki page just explains this magical behavior, right? ). As programmers keeps working on iterators, blocks and functional idioms they understand how easy it is to define your behavior without having the overhead of extra utilities. And this makes ruby programming so easy because you concentrate on getting the job done, not on building scaffolding to support the language itself.&amp;lt;ref&amp;gt;&amp;quot;Ruby Blocks, Iterators and containers&amp;quot; http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67208</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67208"/>
		<updated>2012-10-04T02:37:54Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed crazy, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Functional Idioms =&lt;br /&gt;
&lt;br /&gt;
Functional idioms refers to the various constructs in ruby that mimics Functional Programming Language. Most programmers argue that Ruby is actually an Object Oriented Programming Language and it does not behaves as per the functional programming paradigm. However, thought the argue is open ended, Ruby is a multi-paradigm language that supports a functional style of programming, (supporting functional style of programming doesn't make the language a Functional Programming language ). Ruby does supports the functional programming behavior of avoiding mutable states. To make the concept clear, consider the example shown below which uses the iterator &amp;quot;&amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;quot;. This type of iteration in ruby is actually a function because ruby passes a function ( block of code ) to be called on each item in the collection.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a pythagorean triple!&amp;quot;&lt;br /&gt;
    [3, 4, 5].each { |num| print num }&lt;br /&gt;
&lt;br /&gt;
In the above example, the function being passed is { |num| puts num }. In fact, the code shown below accomplishes the same behavior as done above:&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a pythagorean triple!&amp;quot;&lt;br /&gt;
    my_print_function = lambda { |num| print num }&lt;br /&gt;
    [3, 4, 5].each &amp;amp;my_print_function&lt;br /&gt;
&lt;br /&gt;
Here, in the example shown above, we use the address of &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot; to identify the lines of code that needs to be executed and for each element of the collection execute the function &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot;. So, in that respect, we can call the iterator &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; a functional idiom. &lt;br /&gt;
&lt;br /&gt;
Now, this is very much from the functional programming paradigm, right? ( may be or may be not, lets keep this job for the debaters!). Readers are encouraged to please refer to [http://khelll.com/blog/ruby/ruby-and-functional-programming this] for further understanding of functional idioms.&lt;br /&gt;
&lt;br /&gt;
For curious readers, both the above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    This is a pythagorean triple!&lt;br /&gt;
    [3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Ruby provides such a flexible approach to use iterators as compared to C++ and Java that it is worth learning these iterators and try out them while writing code ( or just for fun if programmers want to!). In ruby the iterator is simply a method that calls yield and passes control to a block of code associated with this iterator method. Unlike Java and C++, ruby does not requires the need to generate helper classes to carry the iterator state thus making it a transparent language. A beginner in ruby may think that the working of a block and yield is magical but actually, it is not! ( the wiki page just explains this magical behavior, right? ). As programmers keeps working on iterators, blocks and functional idioms they understand how easy it is to define your behavior without having the overhead of extra utilities. And this makes ruby programming so easy because you concentrate on getting the job done, not on building scaffolding to support the language itself.&amp;lt;ref&amp;gt;&amp;quot;Ruby Blocks, Iterators and containers&amp;quot; http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67199</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67199"/>
		<updated>2012-10-04T02:33:57Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Functional Idioms */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed crazy, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Functional Idioms =&lt;br /&gt;
&lt;br /&gt;
Functional idioms refers to the various constructs in ruby that mimics Functional Programming Language. Most programmers argue that Ruby is actually an Object Oriented Programming Language and it does not behaves as per the functional programming paradigm. However, thought the argue is open ended, Ruby is a multi-paradigm language that supports a functional style of programming, (supporting functional style of programming doesn't make the language a Functional Programming language ). Ruby does supports the functional programming behavior of avoiding mutable states. To make the concept clear, consider the example shown below which uses the iterator &amp;quot;&amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;quot;. This type of iteration in ruby is actually a function because ruby passes a function ( block of code ) to be called on each item in the collection.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a pythagorean triple!&amp;quot;&lt;br /&gt;
    [3, 4, 5].each { |num| print num }&lt;br /&gt;
&lt;br /&gt;
In the above example, the function being passed is { |num| puts num }. In fact, the code shown below accomplishes the same behavior as done above:&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a pythagorean triple!&amp;quot;&lt;br /&gt;
    my_print_function = lambda { |num| print num }&lt;br /&gt;
    [3, 4, 5].each &amp;amp;my_print_function&lt;br /&gt;
&lt;br /&gt;
Here, in the example shown above, we use the address of &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot; to identify the lines of code that needs to be executed and for each element of the collection execute the function &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot;. So, in that respect, we can call the iterator &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; a functional idiom. &lt;br /&gt;
&lt;br /&gt;
Now, this is very much from the functional programming paradigm, right? ( may be or may be not, lets keep this job for the debaters!). Readers are encouraged to please refer to &amp;lt;a ref=&amp;quot;http://khelll.com/blog/ruby/ruby-and-functional-programming/&amp;quot; /&amp;gt; for further understanding of functional idioms.&lt;br /&gt;
&lt;br /&gt;
For curious readers, both the above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    This is a pythagorean triple!&lt;br /&gt;
    [3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Ruby provides such a flexible approach to use iterators as compared to C++ and Java that it is worth learning these iterators and try out them while writing code ( or just for fun if programmers want to!). In ruby the iterator is simply a method that calls yield and passes control to a block of code associated with this iterator method. Unlike Java and C++, ruby does not requires the need to generate helper classes to carry the iterator state thus making it a transparent language. A beginner in ruby may think that the working of a block and yield is magical but actually, it is not! ( the wiki page just explains this magical behavior, right? ). As programmers keeps working on iterators, blocks and functional idioms they understand how easy it is to define your behavior without having the overhead of extra utilities. And this makes ruby programming so easy because you concentrate on getting the job done, not on building scaffolding to support the language itself.&amp;lt;ref&amp;gt;&amp;quot;Ruby Blocks, Iterators and containers&amp;quot; http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67195</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w58_am&amp;diff=67195"/>
		<updated>2012-10-04T02:23:45Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: Created page with &amp;quot;='''Ruby Blocks, Iterators, Functional Idioms'''=  This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;  __TOC__  = In...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed crazy, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Functional Idioms =&lt;br /&gt;
&lt;br /&gt;
Functional idioms refers to the various constructs in ruby that mimics Functional Programming Language. Most programmers argue that Ruby is actually an Object Oriented Programming Language and it does not behaves as per the functional programming paradigm. However, thought the argue is open ended, Ruby is a multi-paradigm language that supports a functional style of programming, (supporting functional style of programming doesn't make the language a Functional Programming language ). Ruby does supports the functional programming behavior of avoiding mutable states. To make the concept clear, consider the example shown below which uses the iterator &amp;quot;&amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;quot;. This type of iteration in ruby is actually a function because ruby passes a function ( block of code ) to be called on each item in the collection.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a pythagorean triple!&amp;quot;&lt;br /&gt;
    [3, 4, 5].each { |num| print num }&lt;br /&gt;
&lt;br /&gt;
In the above example, the function being passed is { |num| puts num }. In fact, the code shown below accomplishes the same behavior as done above:&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;This is a pythagorean triple!&amp;quot;&lt;br /&gt;
    my_print_function = lambda { |num| print num }&lt;br /&gt;
    [3, 4, 5].each &amp;amp;my_print_function&lt;br /&gt;
&lt;br /&gt;
Here, in the example shown above, we use the address of &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot; to identify the lines of code that needs to be executed and for each element of the collection execute the function &amp;quot;&amp;lt;i&amp;gt;my_print_function&amp;lt;/i&amp;gt;&amp;quot;. So, in that respect, we can call the iterator &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; a functional idiom. &lt;br /&gt;
&lt;br /&gt;
Now, this is very much from the functional programming paradigm, right? ( may be or may be not, lets keep this job for the debaters!). Readers are encouraged to please refer to ___ for further understanding of functional idioms.&lt;br /&gt;
&lt;br /&gt;
For curious readers, both the above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    This is a pythagorean triple!&lt;br /&gt;
    [3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Ruby provides such a flexible approach to use iterators as compared to C++ and Java that it is worth learning these iterators and try out them while writing code ( or just for fun if programmers want to!). In ruby the iterator is simply a method that calls yield and passes control to a block of code associated with this iterator method. Unlike Java and C++, ruby does not requires the need to generate helper classes to carry the iterator state thus making it a transparent language. A beginner in ruby may think that the working of a block and yield is magical but actually, it is not! ( the wiki page just explains this magical behavior, right? ). As programmers keeps working on iterators, blocks and functional idioms they understand how easy it is to define your behavior without having the overhead of extra utilities. And this makes ruby programming so easy because you concentrate on getting the job done, not on building scaffolding to support the language itself.&amp;lt;ref&amp;gt;&amp;quot;Ruby Blocks, Iterators and containers&amp;quot; http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66781</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66781"/>
		<updated>2012-10-03T23:56:48Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed crazy, isn't it?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Ruby provides such a flexible approach to use iterators as compared to C++ and Java that it is worth learning these iterators and try out them while writing code ( or just for fun if programmers want to!). In ruby the iterator is simply a method that calls yield and passes control to a block of code associated with this iterator method. Unlike Java and C++, ruby does not requires the need to generate helper classes to carry the iterator state thus making it a transparent language. A beginner in ruby may think that the working of a block and yield is magical but actually, it is not! ( the wiki page just explains this magical behavior, right? ). As programmers keeps working on iterators, blocks and functional idioms they understand how easy it is to define your behavior without having the overhead of extra utilities. And this makes ruby programming so easy because you concentrate on getting the job done, not on building scaffolding to support the language itself.&amp;lt;ref&amp;gt;&amp;quot;Ruby Blocks, Iterators and containers&amp;quot; http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66701</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66701"/>
		<updated>2012-10-03T23:31:05Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Blocks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;&amp;quot;Blocks basics&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed &amp;lt;i&amp;gt;crazy&amp;lt;/i&amp;gt;, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66698</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66698"/>
		<updated>2012-10-03T23:30:19Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Expression Orientation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;&amp;quot;SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms &amp;quot; http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed crazy, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66691</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66691"/>
		<updated>2012-10-03T23:28:09Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Expression Orientation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation&amp;lt;ref name=&amp;quot;saas_video&amp;quot;&amp;gt;http://www.youtube.com/watch?v=bRn91_Zonh4&amp;lt;/ref&amp;gt; in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed crazy, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66689</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66689"/>
		<updated>2012-10-03T23:27:30Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
Expression orientation in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that. So, for the code shown below:&lt;br /&gt;
&lt;br /&gt;
    a = [ 9, 8, 3, 6, 4]&lt;br /&gt;
    print a.sort&lt;br /&gt;
    print a&lt;br /&gt;
&lt;br /&gt;
produces the following output:&lt;br /&gt;
&lt;br /&gt;
    [3, 4, 6, 8, 9]&lt;br /&gt;
    [9, 8, 3, 6, 4]&lt;br /&gt;
&lt;br /&gt;
From the output it is very clear that the original array is not modified while we can still perform a series of operations on the array &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt;. Surprisingly, ruby also performs cascaded operations for ex,&lt;br /&gt;
&lt;br /&gt;
    x = [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
    print x.uniq.reverse&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
this code gives the following output:&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;I&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;pancakes&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Here, the first operation &amp;lt;i&amp;gt;x.sort&amp;lt;/i&amp;gt; removes duplicate occurrences of string &amp;quot;pancakes&amp;quot; from the temporary array which it creates to perform this operation and then the &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation reverses that temporary array to print the elements in the reverse order. However, the original array is still not modified and the next &amp;lt;i&amp;gt;print.x&amp;lt;/i&amp;gt; statement prints the original array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
One important thing to note here is that if directly &amp;lt;i&amp;gt;reverse&amp;lt;/i&amp;gt; operation is used then the array gets modified. For example, for the code above, if later the programmer directly writes &lt;br /&gt;
&lt;br /&gt;
    print x.reverse!&lt;br /&gt;
    print x&lt;br /&gt;
&lt;br /&gt;
then we get the following output&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
    [&amp;quot;pancakes&amp;quot;, &amp;quot;only&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;pancakes&amp;quot;, &amp;quot;love&amp;quot;, &amp;quot;I&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Thus, the array &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt; gets modified. Ruby is indeed crazy, isn't it?&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66658</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66658"/>
		<updated>2012-10-03T23:14:33Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.youtube.com/watch?v=bRn91_Zonh4 Youtube Saas Video] : Saas Video for the topic.&lt;br /&gt;
&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : Ruby loops and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66657</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66657"/>
		<updated>2012-10-03T23:13:57Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.youtube.com/watch?v=bRn91_Zonh4 Youtube Saas Video] : Saas Video for the topic&lt;br /&gt;
&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : More about loops and iterators in ruby&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;br /&gt;
&lt;br /&gt;
[http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Ruby cookbook] : More about Ruby iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66636</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66636"/>
		<updated>2012-10-03T22:59:03Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.youtube.com/watch?v=bRn91_Zonh4 Youtube Saas Video] : Saas Video for the topic&lt;br /&gt;
&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : More about loops and iterators in ruby&lt;br /&gt;
&lt;br /&gt;
[http://allaboutruby.wordpress.com/2006/01/20/ruby-blocks-101/ Ruby block basics] : Basics about ruby blocks&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66618</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66618"/>
		<updated>2012-10-03T22:46:26Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Blocks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;/&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Expression Orientation =&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.youtube.com/watch?v=bRn91_Zonh4 Youtube Saas Video] : Saas Video for the topic&lt;br /&gt;
&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : More about loops and iterators in ruby&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66616</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66616"/>
		<updated>2012-10-03T22:46:02Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.youtube.com/watch?v=bRn91_Zonh4 Youtube Saas Video] : Saas Video for the topic&lt;br /&gt;
&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : More about loops and iterators in ruby&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66598</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66598"/>
		<updated>2012-10-03T22:38:39Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.youtube.com/watch?v=bRn91_Zonh4 Youtube Saas Video] : Saas Video for the topic&lt;br /&gt;
&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html Loops and Iterators] : More about loops and iterators in ruby&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html Iterators and Blocks] : Read more about blocks and iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66593</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66593"/>
		<updated>2012-10-03T22:37:36Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
[http://www.youtube.com/watch?v=bRn91_Zonh4] : Saas Video for the topic&lt;br /&gt;
&lt;br /&gt;
[http://www.wellho.net/solutions/ruby-conditionals-loops-and-iterators-in-ruby.html] : More about loops and iterators in ruby&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html] : Read more about blocks and iterators.&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66565</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66565"/>
		<updated>2012-10-03T22:26:35Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block&amp;lt;ref name=&amp;quot;blocks&amp;quot;&amp;gt; consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax&amp;lt;ref&amp;gt;&amp;quot;More Afvanced ruby&amp;quot; http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered.&amp;lt;ref&amp;gt;http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/&amp;lt;/ref&amp;gt; A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above&amp;lt;ref name=&amp;quot;own_iterate&amp;quot;&amp;gt;&amp;quot;Defining your own iterators&amp;quot; http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/&amp;lt;/ref&amp;gt;. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator&amp;lt;ref name=own_iterate&amp;quot;/&amp;gt;, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66523</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66523"/>
		<updated>2012-10-03T21:43:51Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered. A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66521</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66521"/>
		<updated>2012-10-03T21:42:55Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref name =&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered. A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66518</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66518"/>
		<updated>2012-10-03T21:40:58Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;Iterators&amp;quot;/&amp;gt;&amp;lt;ref &amp;quot;tutorial_point_i&amp;quot;&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref name=&amp;quot;tutorial_point_i&amp;quot;/&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered. A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66501</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66501"/>
		<updated>2012-10-03T21:25:06Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby for Newbies: Iterators and Blocks&amp;quot; http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-iterators-and-blocks/&amp;lt;/ref&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered. A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
    &lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66495</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66495"/>
		<updated>2012-10-03T21:13:25Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. &amp;lt;i&amp;gt;Step&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt;&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The collect method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered. A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
&lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66494</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66494"/>
		<updated>2012-10-03T21:11:33Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.&amp;lt;ref&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt; Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it.&amp;lt;ref&amp;gt;&amp;quot;Ruby blocks&amp;quot; http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt; For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt;: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt;:&amp;lt;br&amp;gt;===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt;:&amp;lt;br&amp;gt;===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. Step iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;&amp;quot;Ruby Iterators&amp;quot; http://www.tutorialspoint.com/ruby/ruby_iterators.htm&amp;lt;/ref&amp;gt;:&amp;lt;br&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The collect method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered. A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
&lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66492</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66492"/>
		<updated>2012-10-03T21:06:51Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection. Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it. For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. Step iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The collect method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered. A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
&lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66491</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66491"/>
		<updated>2012-10-03T21:06:24Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection. Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it. For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. Step iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The collect method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered. A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
&lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66490</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=66490"/>
		<updated>2012-10-03T21:05:49Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections.&amp;lt;ref&amp;gt;Alan Skorkin. &amp;quot;A wealth of ruby loops and iterators&amp;quot; http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators&amp;lt;/ref&amp;gt; This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection. Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it. For example, &lt;br /&gt;
&lt;br /&gt;
   my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Viewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Viewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. Step iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The collect method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, there are many methods that iterate over a range of values. These iterators are so written that they take a code block as part of their calling syntax and then yield control to that code block. This code block is executed as many times as specified by the iterator. In all of the above examples, the code that is written in curly braces is the block that the iterators yield their control to.&lt;br /&gt;
&lt;br /&gt;
= Blocks =&lt;br /&gt;
A Block consists of a chunk of codes with a name assigned to it. Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax as shown below&lt;br /&gt;
&lt;br /&gt;
    2.times { puts &amp;quot;This is simple a linear block&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
    2.times do&lt;br /&gt;
        puts &amp;quot;This is a block using do..end syntax&amp;quot;&lt;br /&gt;
        puts &amp;quot;You can add multiple statements inside the do...end syntax&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Also, In the curly braces syntax multiple lines of code can be added but they need to be separated with a semicolon.&lt;br /&gt;
&lt;br /&gt;
Block can also take parameters. For example consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    5.times { |i| puts i}&lt;br /&gt;
&lt;br /&gt;
In the above code &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; is passed as a parameter to the block. Thus any thing between the pipe symbols can be considered as parameters. Multiple parameters can also be passed to a block but they need to be separated with commas. For example,&lt;br /&gt;
&lt;br /&gt;
   example = {&amp;quot;one&amp;quot; =&amp;gt; 1, &amp;quot;two&amp;quot; =&amp;gt; 2, &amp;quot;three&amp;quot; =&amp;gt; 3}&lt;br /&gt;
   example.each_pair { |key, value| puts &amp;quot;#{key} : #{value}&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the all the above block examples, an interesting thing to note is that these blocks are not executed until a &amp;lt;i&amp;gt;yield&amp;lt;/i&amp;gt; statement is encountered. A block is always invoked from a function having the same name as that of the defined block. For, examples if there is a block with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt;, then there should be a function with the name &amp;lt;i&amp;gt;myblock&amp;lt;/i&amp;gt; which invokes this block. For example, consider the code shown below:&lt;br /&gt;
&lt;br /&gt;
    def block_testing&lt;br /&gt;
        puts &amp;quot;Inside function block_testing.&amp;quot;&lt;br /&gt;
        yield&lt;br /&gt;
        puts &amp;quot;Back to block_testing function.&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    block_testing { puts &amp;quot;Yielding, to print within block.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Inside function block_testing.&lt;br /&gt;
    Yielding, to print within block.&lt;br /&gt;
    Back to block_testing function.&lt;br /&gt;
&lt;br /&gt;
In the above code, when the block_testing method executes, it prints the first statement, then yield is encountered and the control is passed to the block_testing block which prints the second statement. After the block finishes execution, the control is once again passed to the block_testing function which prints the third statement.&lt;br /&gt;
&lt;br /&gt;
= Defining your own iterator =&lt;br /&gt;
Ruby allows programmers to define their own iterator other than the iterators listed above. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown below:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
        def reverse_iterate&lt;br /&gt;
            if block_given?&lt;br /&gt;
                current_index = self.size-1&lt;br /&gt;
                while current_index &amp;gt;= 0&lt;br /&gt;
                    yield self[current_index]&lt;br /&gt;
                    current_index -= 1&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                print self.reverse&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
In order to use the above iterator, we just need to call this iterator on an array as shown below.&lt;br /&gt;
&lt;br /&gt;
    puts &amp;quot;Fall break begins in 10 secs ...&amp;quot;&lt;br /&gt;
    puts [0,1,2,3,4,5,6,7,8,9, 10].reverse_iterate {|i| puts i }&lt;br /&gt;
    puts &amp;quot;.. yay!! &amp;quot;&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Fall break begins in 10 secs ...&lt;br /&gt;
    10&lt;br /&gt;
    9&lt;br /&gt;
    8&lt;br /&gt;
    7&lt;br /&gt;
    6&lt;br /&gt;
    5&lt;br /&gt;
    4&lt;br /&gt;
    3&lt;br /&gt;
    2&lt;br /&gt;
    1&lt;br /&gt;
    0&lt;br /&gt;
&lt;br /&gt;
    .. yay!!&lt;br /&gt;
&lt;br /&gt;
In the above code, the reverse_iterator iterates the array in reverse order and prints the array elements as shown in the output.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=65965</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w58 am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w58_am&amp;diff=65965"/>
		<updated>2012-09-30T08:59:16Z</updated>

		<summary type="html">&lt;p&gt;Mrkamat: /* The  Step  iterator */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Ruby Blocks, Iterators, Functional Idioms'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Ruby Blocks, Iterators, Functional Idioms.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use DRY principle effectively by using blocks and defining iterators over collections. This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:&lt;br /&gt;
&lt;br /&gt;
Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection. Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.&lt;br /&gt;
A block consists of chunks of codes with a name assigned to it enclosed withing braces. For example, &lt;br /&gt;
&lt;br /&gt;
 my_block { puts &amp;quot;Hello World&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
Functional idioms are ... (please feel free to add about functional idioms here and become the author of it :) )&lt;br /&gt;
&lt;br /&gt;
= Iterators =&lt;br /&gt;
== Conventional Methods ==&lt;br /&gt;
Iterators in ruby can be written in following ways:&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator: &amp;lt;br&amp;gt; ===&lt;br /&gt;
The &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterators works similar to the for loop used in programming languages. As the name suggests it allows to loop over a chunk of code &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
 &lt;br /&gt;
    5.times { puts &amp;quot;Hello Viewers!&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
    Hello Viewers!&lt;br /&gt;
&lt;br /&gt;
Any chunk of code enclosed within the curly braces will execute 5 times in the above example.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; upto &amp;lt;/i&amp;gt; iterator===&lt;br /&gt;
The &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is also similar to the for loop. However, the difference between &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator is that, &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; allows the programmer to specify the start index &amp;lt;i&amp;gt;m&amp;lt;/i&amp;gt; and the end index &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; of the loop. So, if we define an &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; iterator as m.upto(n) { # do some work }, then the code in curly braces will be executed &amp;lt;i&amp;gt;n - m + 1&amp;lt;/i&amp;gt; number of times. For example:&lt;br /&gt;
&lt;br /&gt;
    m = 45&lt;br /&gt;
    n = 48&lt;br /&gt;
    m.utpo(n) {puts &amp;quot;Hello Reviewers! This is really cool, isn't it?&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Hello Reviewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Reviewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Reviewers! This is really cool, isn't it?&lt;br /&gt;
    Hello Reviewers! This is really cool, isn't it?&lt;br /&gt;
&lt;br /&gt;
Here the loop will be executed 48 - 45 + 1 = 4 number of times. If we had used &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; than 45.times would have printed the puts statement 45 number of times. Hence it allows us to make loop execution dynamic as compared to the &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; iterator which is relatively static.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; step &amp;lt;/i&amp;gt; iterator===&lt;br /&gt;
The above iterators &amp;lt;i&amp;gt;times&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;upto&amp;lt;/i&amp;gt; performs the loop execution in increments of 1. Step iterator allows us to vary the increments of loop execution. It gives the programmers the flexibility to skip a few iterations of the loop if it is required as per the programmers logic. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    guess_my_string = &amp;quot;HfezlvlbomWyowrqlad&amp;quot;&lt;br /&gt;
    0.step(guess_my_string.length, 2) {|i| print guess_my_string[i]}&lt;br /&gt;
&lt;br /&gt;
This code produces the following output -&lt;br /&gt;
&lt;br /&gt;
    HelloWorld&lt;br /&gt;
&lt;br /&gt;
In the above code, the variable &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt; collects the iteration number and the print statement prints the character of the String &amp;lt;i&amp;gt;guess_my_string&amp;lt;/i&amp;gt; at position &amp;lt;i&amp;gt;i&amp;lt;/i&amp;gt;. Hence we get the above output.&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt; each &amp;lt;/i&amp;gt; iterator===&lt;br /&gt;
&lt;br /&gt;
All collections in Ruby have an &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method defined, that will allow programmers to go over all of the data members in collection for which the each method is called and do some operation with them. For example, consider the code:&lt;br /&gt;
&lt;br /&gt;
    example = [82, 117, 98, 121, 32, 105, 115, 32, 103, 114, 101, 97, 116, 33]&lt;br /&gt;
    example.each {|i| print i.chr }&lt;br /&gt;
&lt;br /&gt;
The above code prints the following output -&lt;br /&gt;
&lt;br /&gt;
    Ruby is great!&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; iterator takes every element of the array &amp;lt;i&amp;gt;example&amp;lt;/i&amp;gt; and prints its character equivalent. &lt;br /&gt;
&lt;br /&gt;
The each iterator can also be used for hash traversal. Ruby defines the following methods for hash traversal:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This method returns a collection of all the keys present in the hash on which the &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the values present in the hash on which the &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
This method returns a collection of all the key, value pairs present in the hash on which the &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; method was called.&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Consider the example which explains the behavior of all the above methods defined for hash.&lt;br /&gt;
&lt;br /&gt;
    breakfast_menu = {&amp;quot;Martini&amp;quot; =&amp;gt; 4, &amp;quot;apple pie&amp;quot; =&amp;gt; 3, &amp;quot;pan cakes&amp;quot; =&amp;gt; 5 }&lt;br /&gt;
    puts &amp;quot;Today's break fast menu -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_key { |i| puts i }&lt;br /&gt;
    puts &amp;quot;\nTheir respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_value { |i| puts &amp;quot;$#{i}&amp;quot; }&lt;br /&gt;
    puts &amp;quot;\nThe breakfast menu with their respective prices -&amp;quot;&lt;br /&gt;
    breakfast_menu.each_pair { |i, j| puts &amp;quot;#{i}: $#{j}&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
    &lt;br /&gt;
    Today's break fast menu -&lt;br /&gt;
    Martini&lt;br /&gt;
    apple pie&lt;br /&gt;
    pan cakes&lt;br /&gt;
    &lt;br /&gt;
    Their respective prices -&lt;br /&gt;
    $4&lt;br /&gt;
    $3&lt;br /&gt;
    $5&lt;br /&gt;
    &lt;br /&gt;
    The breakfast menu with their respective prices -&lt;br /&gt;
    Martini: $4&lt;br /&gt;
    apple pie: $3&lt;br /&gt;
    pan cakes: $5&lt;br /&gt;
&lt;br /&gt;
In the above code, &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt; defines a hash. It contains key, value pair. When &amp;lt;i&amp;gt;each_key&amp;lt;/i&amp;gt; method is called on &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;, it prints all the keys present in the hash &amp;lt;i&amp;gt;breakfast_menu&amp;lt;/i&amp;gt;. Similarly for &amp;lt;i&amp;gt;each_value&amp;lt;/i&amp;gt;. For &amp;lt;i&amp;gt;each_pair&amp;lt;/i&amp;gt; it prints all the key value pairs present in the hash. Hence the output.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;collect&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
&lt;br /&gt;
This simply returns all the elements of the collection. The collect method works for arrays as well as hashes. For example:&lt;br /&gt;
&lt;br /&gt;
    a = [&amp;quot;ruby&amp;quot;, &amp;quot;rails&amp;quot;, &amp;quot;ruby on rails&amp;quot;];&lt;br /&gt;
    b = Array.new&lt;br /&gt;
    b = a.collect{ |i| i.capitalize}&lt;br /&gt;
    puts b&lt;br /&gt;
&lt;br /&gt;
This produces the following output:&lt;br /&gt;
&lt;br /&gt;
    Ruby&lt;br /&gt;
    Rails&lt;br /&gt;
    Ruby on rails&lt;br /&gt;
&lt;br /&gt;
In the above code, a.collect iterates over all the elements of &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and returns a collection of all elements in &amp;lt;i&amp;gt;a&amp;lt;/i&amp;gt; and assigns it to &amp;lt;i&amp;gt;b&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; iterator ===&lt;br /&gt;
The &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method creates a temporary array and stores in it whatever value is returned from each iteration of the block of code on which the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method is called. Then that temporary array is returned as a collection. For example consider the ruby code&lt;br /&gt;
&lt;br /&gt;
    fruits = [ &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot; ]&lt;br /&gt;
    puts fruits.map { |i| i.reverse }&lt;br /&gt;
&lt;br /&gt;
The above code produces the following output:&lt;br /&gt;
&lt;br /&gt;
    elppa&lt;br /&gt;
    ananab&lt;br /&gt;
    yrrehc&lt;br /&gt;
&lt;br /&gt;
In the above code the &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method iterates over all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; and reverse them. These reverse strings are stored in a temporary array which &amp;lt;i&amp;gt;map&amp;lt;/i&amp;gt; method uses and returns that array when all the elements in &amp;lt;i&amp;gt;fruits&amp;lt;/i&amp;gt; are iterated.&lt;br /&gt;
&lt;br /&gt;
= Topical References =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;/div&gt;</summary>
		<author><name>Mrkamat</name></author>
	</entry>
</feed>