<?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=Cssuich</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=Cssuich"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Cssuich"/>
	<updated>2026-06-08T21:30:24Z</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_2w59_bc&amp;diff=69622</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69622"/>
		<updated>2012-11-16T21:35:29Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Directory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Overview of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx - 2001 - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/ - 2011&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Understand the UML diagram for the Decorator pattern can help you understand the purpose of the Decorator pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.&lt;br /&gt;
&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004 - 2008&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For a general overview and UML diagrams of the Decorator design pattern, we recommend:&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia]&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory]&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers]&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
Understanding the general concepts behind using the Decorator pattern would be the first step for any language implementation. The next step is usually finding examples that show how the pattern can be used, possibly by language. Here we will look at sources that give useful examples for understanding how to implement the Decorator pattern in Ruby. It is also worth noting that all references cited in this wiki are useful for understanding the pattern in general, despite the language that may be used in the examples.&lt;br /&gt;
&lt;br /&gt;
The most popular example used to explain the Decorator pattern in Ruby is an example involving a Coffee class, of which there are various subclasses of coffee that can have various additional ingredients (milk, sugar, etc.). For many of the sites, they mentioned using the &amp;quot;Head First Design Patterns&amp;quot; book as a source, so of course we recommend the same &amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;. The biggest disadvantage to using this resource is that it is a part of a textbook so is rather lengthy. There are a variety of sites devoted to explaining this pattern (most of which use the Coffee example) but a few stood out as especially useful in various ways. One of the better resources for any project is someone that has used the pattern before and blogs are a great place to start. One blog in particular gives a great explanation of the Decorator pattern being used in the Coffee example &amp;lt;ref name=&amp;quot;8lines&amp;quot;&amp;gt; http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html - 2006&amp;lt;/ref&amp;gt;. This is one of the sources that explicitly mentioned using the &amp;quot;Head First Design Patterns&amp;quot; book, so it may be easy to use these resources in parallel.&lt;br /&gt;
&lt;br /&gt;
Another useful resource for implementing the Decorator pattern in Ruby written by another developer is on the site Programming = Fun &amp;lt;ref name=&amp;quot;decorators&amp;quot;&amp;gt; http://kresimirbojcic.com/2011/12/01/decorators-in-ruby.html -2011 &amp;lt;/ref&amp;gt;. One advantage this source has over the previous is that it is more up to date so it is less likely that there will be conflicting information with current knowledge and technology. This site uses the same coffee example but does not mention using the book so this may threaten how valid their interpretation is. Although they are both also about the same length in terms of words and code snippets, it seems that this one is more detailed in explaining what they did and why as they present the relevant code snippets. A disadvantage to using either of these resources is that if you are looking for variety or different ways of implementing the pattern they do not offer this information.&lt;br /&gt;
&lt;br /&gt;
If you are not sure how you want to use the Decorator but you believe you may want to use it, then the explanations given on Thoughtbot might be useful &amp;lt;ref name=&amp;quot;alternatives&amp;quot;&amp;gt; http://robots.thoughtbot.com/post/14825364877/evaluating-alternative-decorator-implementations-in - 2011 &amp;lt;/ref&amp;gt;. This source also uses the Coffee example, however looks at various ways of implementing the coffee example using the Decorator pattern. This source may be the most useful for examples because of the fact that it includes the example from the &amp;quot;Head First Design Patterns&amp;quot; book but also includes other alternatives that most other sites do not consider or explain. This source also has the advantage that is uses a good mix of textual explanations and code snippets to get the concepts across. A disadvantage is that if you are wanting more in depth explanations on the alternative methods they describe it may be difficult to find other sources that use similar examples but also explain them in more detail.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For Ruby resources, we recommend:&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
*[http://kresimirbojcic.com/2011/12/01/decorators-in-ruby.html  Decorators]&lt;br /&gt;
*[http://robots.thoughtbot.com/post/14825364877/evaluating-alternative-decorator-implementations-in  Alternatives]&lt;br /&gt;
*[http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html Confessions of Ruby Programmer]&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt;; in this book an in-depth explanation of the Decorator Pattern can be found. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.&lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional developers&amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012?&amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern as well as textual explanations if needed. An advantage to using this resource is that it uses less text and more examples to show how the Decorator pattern can be used. It is also helpful that this site has multiple examples that can help increase understanding through reinforcement and multiple viewpoints (and languages). A disadvantage to using this site is that all of the examples are general examples using the pattern as opposed to real life situations or scenarios (like the Coffee example). If it is more useful for you to have a more in-depth walk through of a general example, then using the &amp;quot;Decorator Pattern by Example&amp;quot; page may be more helpful &amp;lt;ref name=&amp;quot;dpexample&amp;quot;&amp;gt; http://www.agiledeveloper.com/articles/DecoratorByExample.pdf - ? &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some sites use over-arching real life examples to explain the Decorator pattern and its usage. The most popular of these examples (in any language) is the Coffee class mentioned previously, however there are other examples based on real life scenarios and situations that can relay the same information. One that seems to be useful in conveying the idea behind the Decorator pattern is using the pattern to report sports statistics for a given team. One site uses the example of a developer hired to create software that reports Soccer sports statistics &amp;lt;ref name=&amp;quot;soccer&amp;quot;&amp;gt;http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ - 2012 &amp;lt;/ref&amp;gt;. This site uses a mix of textual explanations and examples to explain the pattern. One advantage to using this site is that it is relatively short and includes a real world example. It also provides the code in a way that would make it simple to attempt to run it yourself and see how it works. A disadvantage for this resource is that it does not break down exactly how it got all of its code so it may be difficult to understand how to get going on your own using your own &amp;quot;problem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Although most examples for this pattern focus on other usages, it is possible to use the Decorator pattern with GUIs (Graphical User Interfaces). A useful example for understanding this usage can be found on OODesign.com &amp;lt;ref name=&amp;quot;gui&amp;quot;&amp;gt;http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html - ?&amp;lt;/ref&amp;gt;. This site uses code and UML diagrams to explain the usage of Decorator when implementing GUIs. There are small textual explanations in between code snippets but the bulk of the page is code. This site is useful because it offers code that can easily be tested and/or modified if needed to gain a better understanding. Another advantage is that there is not much that has to be read. A disadvantage is that if the code given is not similar to what you are trying to achieve it may be difficult to understand whether you need to be using the Decorator pattern for you particular GUI; outside sources may be needed for this understanding.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For Java and C# resources, we recommend:&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking]&lt;br /&gt;
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ blog.image0]&lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Summary: Typical Wiki page&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory - Decorator Design Pattern]&lt;br /&gt;
**Summary: Quick reference of UML and participants&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern]&lt;br /&gt;
**Summary: Ice Cream example in Java&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Summary: Textbook chapter&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
*[http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html confessions of a ruby programmer]&lt;br /&gt;
**Summary: Coffee example in Ruby with lots of code as well as discussion.&lt;br /&gt;
**Pro: Lots of source code to look through and a helpful overview of Decorator&lt;br /&gt;
**Con: Provides a common implementation - nothing outside of the box.&lt;br /&gt;
*[http://kresimirbojcic.com/2011/12/01/decorators-in-ruby.html Decorators in Ruby]&lt;br /&gt;
**Summary: Another coffee example in Ruby with lots of code and discussion.&lt;br /&gt;
**Pro: Also has lots of source code to look through and help with Decorator implementations in Ruby.&lt;br /&gt;
**Con: Also provides a common implementation - nothing outside of the box.&lt;br /&gt;
*[http://robots.thoughtbot.com/post/14825364877/evaluating-alternative-decorator-implementations-in Evaluating alternative Decorator implementations in Ruby]&lt;br /&gt;
**Summary: Provides an overview of the normal implementation and some alternative implementations of Decorator in Ruby.&lt;br /&gt;
**Pro: Lots of code samples on various implementations. This is a more advanced resource than just understanding the basics of the pattern.&lt;br /&gt;
**Con: A bit lengthier, but that comes with the advanced nature of the article.&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking - Decorator Design Pattern]&lt;br /&gt;
**Summary: Window example with Java snippets, checklist and rules of thumb.&lt;br /&gt;
**Pro: This page presents an interesting concrete example with UML diagrams and code snippets. At the end, it covers a checklist for using the Decorator pattern as well as some rules of thumb.&lt;br /&gt;
**Con: The code provided in this resource are just snippets, not a full example.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Fwww.agiledeveloper.com%2Farticles%2FDecoratorByExample.pdf Decorator Pattern by Example]&lt;br /&gt;
**Summary: Scholarly paper discussing the usage of Decorator pattern Java with a Registration example.&lt;br /&gt;
**Pro: Very detailed and complete code.&lt;br /&gt;
**Con: Longer and very technical, but this comes with the advanced nature/scholarly paper nature.&lt;br /&gt;
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ Design Pattern Series]&lt;br /&gt;
**Summary: Covers Decorator pattern in C# with a soccer statistics example.&lt;br /&gt;
**Pro: Plenty of code with a real world example and discusses implementation quite well.&lt;br /&gt;
**Con: Doesn't go into too much detail with the concept and how they developed their code.&lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
**Summary: Code intensive GUI example in Java&lt;br /&gt;
**Pro: The UML diagram is nice and the code is quite complete. The concrete example is applicable to the real world.&lt;br /&gt;
**Con: The page does not provide too much explanation, mainly just a code sample.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69620</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69620"/>
		<updated>2012-11-16T21:31:53Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Directory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Overview of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx - 2001 - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/ - 2011&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Understand the UML diagram for the Decorator pattern can help you understand the purpose of the Decorator pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.&lt;br /&gt;
&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004 - 2008&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For a general overview and UML diagrams of the Decorator design pattern, we recommend:&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia]&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory]&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers]&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
Understanding the general concepts behind using the Decorator pattern would be the first step for any language implementation. The next step is usually finding examples that show how the pattern can be used, possibly by language. Here we will look at sources that give useful examples for understanding how to implement the Decorator pattern in Ruby. It is also worth noting that all references cited in this wiki are useful for understanding the pattern in general, despite the language that may be used in the examples.&lt;br /&gt;
&lt;br /&gt;
The most popular example used to explain the Decorator pattern in Ruby is an example involving a Coffee class, of which there are various subclasses of coffee that can have various additional ingredients (milk, sugar, etc.). For many of the sites, they mentioned using the &amp;quot;Head First Design Patterns&amp;quot; book as a source, so of course we recommend the same &amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;. The biggest disadvantage to using this resource is that it is a part of a textbook so is rather lengthy. There are a variety of sites devoted to explaining this pattern (most of which use the Coffee example) but a few stood out as especially useful in various ways. One of the better resources for any project is someone that has used the pattern before and blogs are a great place to start. One blog in particular gives a great explanation of the Decorator pattern being used in the Coffee example &amp;lt;ref name=&amp;quot;8lines&amp;quot;&amp;gt; http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html - 2006&amp;lt;/ref&amp;gt;. This is one of the sources that explicitly mentioned using the &amp;quot;Head First Design Patterns&amp;quot; book, so it may be easy to use these resources in parallel.&lt;br /&gt;
&lt;br /&gt;
Another useful resource for implementing the Decorator pattern in Ruby written by another developer is on the site Programming = Fun &amp;lt;ref name=&amp;quot;decorators&amp;quot;&amp;gt; http://kresimirbojcic.com/2011/12/01/decorators-in-ruby.html -2011 &amp;lt;/ref&amp;gt;. One advantage this source has over the previous is that it is more up to date so it is less likely that there will be conflicting information with current knowledge and technology. This site uses the same coffee example but does not mention using the book so this may threaten how valid their interpretation is. Although they are both also about the same length in terms of words and code snippets, it seems that this one is more detailed in explaining what they did and why as they present the relevant code snippets. A disadvantage to using either of these resources is that if you are looking for variety or different ways of implementing the pattern they do not offer this information.&lt;br /&gt;
&lt;br /&gt;
If you are not sure how you want to use the Decorator but you believe you may want to use it, then the explanations given on Thoughtbot might be useful &amp;lt;ref name=&amp;quot;alternatives&amp;quot;&amp;gt; http://robots.thoughtbot.com/post/14825364877/evaluating-alternative-decorator-implementations-in - 2011 &amp;lt;/ref&amp;gt;. This source also uses the Coffee example, however looks at various ways of implementing the coffee example using the Decorator pattern. This source may be the most useful for examples because of the fact that it includes the example from the &amp;quot;Head First Design Patterns&amp;quot; book but also includes other alternatives that most other sites do not consider or explain. This source also has the advantage that is uses a good mix of textual explanations and code snippets to get the concepts across. A disadvantage is that if you are wanting more in depth explanations on the alternative methods they describe it may be difficult to find other sources that use similar examples but also explain them in more detail.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For Ruby resources, we recommend:&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
*[http://kresimirbojcic.com/2011/12/01/decorators-in-ruby.html  Decorators]&lt;br /&gt;
*[http://robots.thoughtbot.com/post/14825364877/evaluating-alternative-decorator-implementations-in  Alternatives]&lt;br /&gt;
*[http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html Confessions of Ruby Programmer]&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt;; in this book an in-depth explanation of the Decorator Pattern can be found. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.&lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional developers&amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012?&amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern as well as textual explanations if needed. An advantage to using this resource is that it uses less text and more examples to show how the Decorator pattern can be used. It is also helpful that this site has multiple examples that can help increase understanding through reinforcement and multiple viewpoints (and languages). A disadvantage to using this site is that all of the examples are general examples using the pattern as opposed to real life situations or scenarios (like the Coffee example). If it is more useful for you to have a more in-depth walk through of a general example, then using the &amp;quot;Decorator Pattern by Example&amp;quot; page may be more helpful &amp;lt;ref name=&amp;quot;dpexample&amp;quot;&amp;gt; http://www.agiledeveloper.com/articles/DecoratorByExample.pdf - ? &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some sites use over-arching real life examples to explain the Decorator pattern and its usage. The most popular of these examples (in any language) is the Coffee class mentioned previously, however there are other examples based on real life scenarios and situations that can relay the same information. One that seems to be useful in conveying the idea behind the Decorator pattern is using the pattern to report sports statistics for a given team. One site uses the example of a developer hired to create software that reports Soccer sports statistics &amp;lt;ref name=&amp;quot;soccer&amp;quot;&amp;gt;http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ - 2012 &amp;lt;/ref&amp;gt;. This site uses a mix of textual explanations and examples to explain the pattern. One advantage to using this site is that it is relatively short and includes a real world example. It also provides the code in a way that would make it simple to attempt to run it yourself and see how it works. A disadvantage for this resource is that it does not break down exactly how it got all of its code so it may be difficult to understand how to get going on your own using your own &amp;quot;problem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Although most examples for this pattern focus on other usages, it is possible to use the Decorator pattern with GUIs (Graphical User Interfaces). A useful example for understanding this usage can be found on OODesign.com &amp;lt;ref name=&amp;quot;gui&amp;quot;&amp;gt;http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html - ?&amp;lt;/ref&amp;gt;. This site uses code and UML diagrams to explain the usage of Decorator when implementing GUIs. There are small textual explanations in between code snippets but the bulk of the page is code. This site is useful because it offers code that can easily be tested and/or modified if needed to gain a better understanding. Another advantage is that there is not much that has to be read. A disadvantage is that if the code given is not similar to what you are trying to achieve it may be difficult to understand whether you need to be using the Decorator pattern for you particular GUI; outside sources may be needed for this understanding.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For Java and C# resources, we recommend:&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking]&lt;br /&gt;
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ blog.image0]&lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Summary: Typical Wiki page&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory - Decorator Design Pattern]&lt;br /&gt;
**Summary: Quick reference of UML and participants&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern]&lt;br /&gt;
**Summary: Ice Cream example in Java&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Summary: Textbook chapter&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
*[http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html confessions of a ruby programmer]&lt;br /&gt;
**Summary: Coffee example in Ruby with lots of code as well as discussion.&lt;br /&gt;
**Pro: Lots of source code to look through and a helpful overview of Decorator&lt;br /&gt;
**Con: Provides a common implementation - nothing outside of the box.&lt;br /&gt;
*[http://kresimirbojcic.com/2011/12/01/decorators-in-ruby.html Decorators in Ruby]&lt;br /&gt;
**Summary: Another coffee example in Ruby with lots of code and discussion.&lt;br /&gt;
**Pro: Also has lots of source code to look through and help with Decorator implementations in Ruby.&lt;br /&gt;
**Con: Also provides a common implementation - nothing outside of the box.&lt;br /&gt;
*[http://robots.thoughtbot.com/post/14825364877/evaluating-alternative-decorator-implementations-in Evaluating alternative Decorator implementations in Ruby]&lt;br /&gt;
**Summary: Provides an overview of the normal implementation and some alternative implementations of Decorator in Ruby.&lt;br /&gt;
**Pro: Lots of code samples on various implementations. This is a more advanced resource than just understanding the basics of the pattern.&lt;br /&gt;
**Con: A bit lengthier, but that comes with the advanced nature of the article.&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking - Decorator Design Pattern]&lt;br /&gt;
**Summary: Window example with Java snippets, checklist and rules of thumb&lt;br /&gt;
**Pro: This page presents an interesting concrete example with UML diagrams and code snippets. At the end, it covers a checklist for using the Decorator pattern as well as some rules of thumb.&lt;br /&gt;
**Con: The code provided in this resource are just snippets, not a full example.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Fwww.agiledeveloper.com%2Farticles%2FDecoratorByExample.pdf Decorator Pattern by Example]&lt;br /&gt;
**Summary: &lt;br /&gt;
**Pro: &lt;br /&gt;
**Con: &lt;br /&gt;
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ Design Pattern Series]&lt;br /&gt;
**Summary: &lt;br /&gt;
**Pro: &lt;br /&gt;
**Con: &lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
**Summary: Code intensive GUI example in Java&lt;br /&gt;
**Pro: The UML diagram is nice and the code is quite complete. The concrete example is applicable to the real world.&lt;br /&gt;
**Con: The page does not provide too much explanation, mainly just a code sample.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69619</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69619"/>
		<updated>2012-11-16T21:30:40Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Directory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Overview of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx - 2001 - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/ - 2011&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Understand the UML diagram for the Decorator pattern can help you understand the purpose of the Decorator pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.&lt;br /&gt;
&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004 - 2008&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For a general overview and UML diagrams of the Decorator design pattern, we recommend:&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia]&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory]&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers]&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
Understanding the general concepts behind using the Decorator pattern would be the first step for any language implementation. The next step is usually finding examples that show how the pattern can be used, possibly by language. Here we will look at sources that give useful examples for understanding how to implement the Decorator pattern in Ruby. It is also worth noting that all references cited in this wiki are useful for understanding the pattern in general, despite the language that may be used in the examples.&lt;br /&gt;
&lt;br /&gt;
The most popular example used to explain the Decorator pattern in Ruby is an example involving a Coffee class, of which there are various subclasses of coffee that can have various additional ingredients (milk, sugar, etc.). For many of the sites, they mentioned using the &amp;quot;Head First Design Patterns&amp;quot; book as a source, so of course we recommend the same &amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;. The biggest disadvantage to using this resource is that it is a part of a textbook so is rather lengthy. There are a variety of sites devoted to explaining this pattern (most of which use the Coffee example) but a few stood out as especially useful in various ways. One of the better resources for any project is someone that has used the pattern before and blogs are a great place to start. One blog in particular gives a great explanation of the Decorator pattern being used in the Coffee example &amp;lt;ref name=&amp;quot;8lines&amp;quot;&amp;gt; http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html - 2006&amp;lt;/ref&amp;gt;. This is one of the sources that explicitly mentioned using the &amp;quot;Head First Design Patterns&amp;quot; book, so it may be easy to use these resources in parallel.&lt;br /&gt;
&lt;br /&gt;
Another useful resource for implementing the Decorator pattern in Ruby written by another developer is on the site Programming = Fun &amp;lt;ref name=&amp;quot;decorators&amp;quot;&amp;gt; http://kresimirbojcic.com/2011/12/01/decorators-in-ruby.html -2011 &amp;lt;/ref&amp;gt;. One advantage this source has over the previous is that it is more up to date so it is less likely that there will be conflicting information with current knowledge and technology. This site uses the same coffee example but does not mention using the book so this may threaten how valid their interpretation is. Although they are both also about the same length in terms of words and code snippets, it seems that this one is more detailed in explaining what they did and why as they present the relevant code snippets. A disadvantage to using either of these resources is that if you are looking for variety or different ways of implementing the pattern they do not offer this information.&lt;br /&gt;
&lt;br /&gt;
If you are not sure how you want to use the Decorator but you believe you may want to use it, then the explanations given on Thoughtbot might be useful &amp;lt;ref name=&amp;quot;alternatives&amp;quot;&amp;gt; http://robots.thoughtbot.com/post/14825364877/evaluating-alternative-decorator-implementations-in - 2011 &amp;lt;/ref&amp;gt;. This source also uses the Coffee example, however looks at various ways of implementing the coffee example using the Decorator pattern. This source may be the most useful for examples because of the fact that it includes the example from the &amp;quot;Head First Design Patterns&amp;quot; book but also includes other alternatives that most other sites do not consider or explain. This source also has the advantage that is uses a good mix of textual explanations and code snippets to get the concepts across. A disadvantage is that if you are wanting more in depth explanations on the alternative methods they describe it may be difficult to find other sources that use similar examples but also explain them in more detail.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For Ruby resources, we recommend:&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
*[http://kresimirbojcic.com/2011/12/01/decorators-in-ruby.html  Decorators]&lt;br /&gt;
*[http://robots.thoughtbot.com/post/14825364877/evaluating-alternative-decorator-implementations-in  Alternatives]&lt;br /&gt;
*[http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html Confessions of Ruby Programmer]&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt;; in this book an in-depth explanation of the Decorator Pattern can be found. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.&lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional developers&amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012?&amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern as well as textual explanations if needed. An advantage to using this resource is that it uses less text and more examples to show how the Decorator pattern can be used. It is also helpful that this site has multiple examples that can help increase understanding through reinforcement and multiple viewpoints (and languages). A disadvantage to using this site is that all of the examples are general examples using the pattern as opposed to real life situations or scenarios (like the Coffee example). If it is more useful for you to have a more in-depth walk through of a general example, then using the &amp;quot;Decorator Pattern by Example&amp;quot; page may be more helpful &amp;lt;ref name=&amp;quot;dpexample&amp;quot;&amp;gt; http://www.agiledeveloper.com/articles/DecoratorByExample.pdf - ? &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some sites use over-arching real life examples to explain the Decorator pattern and its usage. The most popular of these examples (in any language) is the Coffee class mentioned previously, however there are other examples based on real life scenarios and situations that can relay the same information. One that seems to be useful in conveying the idea behind the Decorator pattern is using the pattern to report sports statistics for a given team. One site uses the example of a developer hired to create software that reports Soccer sports statistics &amp;lt;ref name=&amp;quot;soccer&amp;quot;&amp;gt;http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ - 2012 &amp;lt;/ref&amp;gt;. This site uses a mix of textual explanations and examples to explain the pattern. One advantage to using this site is that it is relatively short and includes a real world example. It also provides the code in a way that would make it simple to attempt to run it yourself and see how it works. A disadvantage for this resource is that it does not break down exactly how it got all of its code so it may be difficult to understand how to get going on your own using your own &amp;quot;problem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Although most examples for this pattern focus on other usages, it is possible to use the Decorator pattern with GUIs (Graphical User Interfaces). A useful example for understanding this usage can be found on OODesign.com &amp;lt;ref name=&amp;quot;gui&amp;quot;&amp;gt;http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html - ?&amp;lt;/ref&amp;gt;. This site uses code and UML diagrams to explain the usage of Decorator when implementing GUIs. There are small textual explanations in between code snippets but the bulk of the page is code. This site is useful because it offers code that can easily be tested and/or modified if needed to gain a better understanding. Another advantage is that there is not much that has to be read. A disadvantage is that if the code given is not similar to what you are trying to achieve it may be difficult to understand whether you need to be using the Decorator pattern for you particular GUI; outside sources may be needed for this understanding.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For Java and C# resources, we recommend:&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking]&lt;br /&gt;
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ blog.image0]&lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Summary: Typical Wiki page&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory - Decorator Design Pattern]&lt;br /&gt;
**Summary: Quick reference of UML and participants&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern]&lt;br /&gt;
**Summary: Ice Cream example in Java&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Summary: Textbook chapter&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
*[http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html confessions of a ruby programmer]&lt;br /&gt;
**Summary: Coffee example in Ruby with lots of code as well as discussion.&lt;br /&gt;
**Pro: Lots of source code to look through and a helpful overview of Decorator&lt;br /&gt;
**Con: &lt;br /&gt;
*[http://kresimirbojcic.com/2011/12/01/decorators-in-ruby.html Decorators in Ruby]&lt;br /&gt;
**Summary: Another coffee example in Ruby with lots of code and discussion.&lt;br /&gt;
**Pro: Also has lots of source code to look through and help with Decorator implementations in Ruby.&lt;br /&gt;
**Con: &lt;br /&gt;
*[http://robots.thoughtbot.com/post/14825364877/evaluating-alternative-decorator-implementations-in Evaluating alternative Decorator implementations in Ruby]&lt;br /&gt;
**Summary: Provides an overview of the normal implementation and some alternative implementations of Decorator in Ruby.&lt;br /&gt;
**Pro: Lots of code samples on various implementations. This is a more advanced resource than just understanding the basics of the pattern.&lt;br /&gt;
**Con: A bit lengthier, but that comes with the advanced nature of the article.&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking - Decorator Design Pattern]&lt;br /&gt;
**Summary: Window example with Java snippets, checklist and rules of thumb&lt;br /&gt;
**Pro: This page presents an interesting concrete example with UML diagrams and code snippets. At the end, it covers a checklist for using the Decorator pattern as well as some rules of thumb.&lt;br /&gt;
**Con: The code provided in this resource are just snippets, not a full example.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Fwww.agiledeveloper.com%2Farticles%2FDecoratorByExample.pdf Decorator Pattern by Example]&lt;br /&gt;
**Summary: &lt;br /&gt;
**Pro: &lt;br /&gt;
**Con: &lt;br /&gt;
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ Design Pattern Series]&lt;br /&gt;
**Summary: &lt;br /&gt;
**Pro: &lt;br /&gt;
**Con: &lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
**Summary: Code intensive GUI example in Java&lt;br /&gt;
**Pro: The UML diagram is nice and the code is quite complete. The concrete example is applicable to the real world.&lt;br /&gt;
**Con: The page does not provide too much explanation, mainly just a code sample.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69614</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69614"/>
		<updated>2012-11-16T21:24:32Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Directory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Overview of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx - 2001 - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/ - 2011&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Understand the UML diagram for the Decorator pattern can help you understand the purpose of the Decorator pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.&lt;br /&gt;
&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004 - 2008&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For a general overview and UML diagrams of the Decorator design pattern, we recommend:&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia]&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory]&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers]&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
Understanding the general concepts behind using the Decorator pattern would be the first step for any language implementation. The next step is usually finding examples that show how the pattern can be used, possibly by language. Here we will look at sources that give useful examples for understanding how to implement the Decorator pattern in Ruby. It is also worth noting that all references cited in this wiki are useful for understanding the pattern in general, despite the language that may be used in the examples.&lt;br /&gt;
&lt;br /&gt;
The most popular example used to explain the Decorator pattern in Ruby is an example involving a Coffee class, of which there are various subclasses of coffee that can have various additional ingredients (milk, sugar, etc.). For many of the sites, they mentioned using the &amp;quot;Head First Design Patterns&amp;quot; book as a source, so of course we recommend the same &amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;. The biggest disadvantage to using this resource is that it is a part of a textbook so is rather lengthy. There are a variety of sites devoted to explaining this pattern (most of which use the Coffee example) but a few stood out as especially useful in various ways. One of the better resources for any project is someone that has used the pattern before and blogs are a great place to start. One blog in particular gives a great explanation of the Decorator pattern being used in the Coffee example &amp;lt;ref name=&amp;quot;8lines&amp;quot;&amp;gt; http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html - 2006&amp;lt;/ref&amp;gt;. This is one of the sources that explicitly mentioned using the &amp;quot;Head First Design Patterns&amp;quot; book, so it may be easy to use these resources in parallel.&lt;br /&gt;
&lt;br /&gt;
Another useful resource for implementing the Decorator pattern in Ruby written by another developer is on the site Programming = Fun &amp;lt;ref name=&amp;quot;decorators&amp;quot;&amp;gt; http://kresimirbojcic.com/2011/12/01/decorators-in-ruby.html -2011 &amp;lt;/ref&amp;gt;. One advantage this source has over the previous is that it is more up to date so it is less likely that there will be conflicting information with current knowledge and technology. This site uses the same coffee example but does not mention using the book so this may threaten how valid their interpretation is. Although they are both also about the same length, it seems that this one is more detailed in explaining what they did and why as they present the relevant code snippets. &lt;br /&gt;
&lt;br /&gt;
If you are not sure how you want to use the Decorator but you believe you may want to use it, then the explanations given on Thoughtbot might be useful &amp;lt;ref name=&amp;quot;alternatives&amp;quot;&amp;gt; http://robots.thoughtbot.com/post/14825364877/evaluating-alternative-decorator-implementations-in - 2011 &amp;lt;/ref&amp;gt;. This source also uses the Coffee example, however looks at various ways of implementing the coffee example using the Decorator pattern. This source may be the most useful for examples because of the fact that it includes the example from the &amp;quot;Head First Design Patterns&amp;quot; book but also includes other alternatives that most other sites do not consider or explain. This source also has the advantage that is uses a good mix of textual explanations and code snippets to get the concepts across. A disadvantage is that if you are wanting more in depth explanations on the alternative methods they describe it may be difficult to find other sources that use similar examples but also explain them in more detail.&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt;; in this book an in-depth explanation of the Decorator Pattern can be found. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.&lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional developers&amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012?&amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern as well as textual explanations if needed. An advantage to using this resource is that it uses less text and more examples to show how the Decorator pattern can be used. It is also helpful that this site has multiple examples that can help increase understanding through reinforcement and multiple viewpoints (and languages). A disadvantage to using this site is that all of the examples are general examples using the pattern as opposed to real life situations or scenarios (like the Coffee example). If it is more useful for you to have a more in-depth walk through of a general example, then using the &amp;quot;Decorator Pattern by Example&amp;quot; page may be more helpful &amp;lt;ref name=&amp;quot;dpexample&amp;quot;&amp;gt; http://www.agiledeveloper.com/articles/DecoratorByExample.pdf - ? &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some sites use over-arching real life examples to explain the Decorator pattern and its usage. The most popular of these examples (in any language) is the Coffee class mentioned previously, however there are other examples based on real life scenarios and situations that can relay the same information. One that seems to be useful in conveying the idea behind the Decorator pattern is using the pattern to report sports statistics for a given team. One site uses the example of a developer hired to create software that reports Soccer sports statistics &amp;lt;ref name=&amp;quot;soccer&amp;quot;&amp;gt;http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ - 2012 &amp;lt;/ref&amp;gt;. This site uses a mix of textual explanations and examples to explain the pattern. One advantage to using this site is that it is relatively short and includes a real world example. It also provides the code in a way that would make it simple to attempt to run it yourself and see how it works. A disadvantage for this resource is that it does not break down exactly how it got all of its code so it may be difficult to understand how to get going on your own using your own &amp;quot;problem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Although most examples for this pattern focus on other usages, it is possible to use the Decorator pattern with GUIs (Graphical User Interfaces). A useful example for understanding this usage can be found on OODesign.com &amp;lt;ref name=&amp;quot;gui&amp;quot;&amp;gt;http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html - ?&amp;lt;/ref&amp;gt;. This site uses code and UML diagrams to explain the usage of Decorator when implementing GUIs. There are small textual explanations in between code snippets but the bulk of the page is code. This site is useful because it offers code that can easily be tested and/or modified if needed to gain a better understanding. Another advantage is that there is not much that has to be read. A disadvantage is that if the code given is not similar to what you are trying to achieve it may be difficult to understand whether you need to be using the Decorator pattern for you particular GUI; outside sources may be needed for this understanding.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For Java and C# resources, we recommend:&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking]&lt;br /&gt;
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ blog.image0]&lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Summary: Typical Wiki page&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory - Decorator Design Pattern]&lt;br /&gt;
**Summary: Quick reference of UML and participants&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern]&lt;br /&gt;
**Summary: Ice Cream example in Java&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Summary: Textbook chapter&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
*[http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html confessions of a ruby programmer]&lt;br /&gt;
**Summary: &lt;br /&gt;
**Pro: &lt;br /&gt;
**Con: &lt;br /&gt;
*[http://kresimirbojcic.com/2011/12/01/decorators-in-ruby.html Decorators in Ruby]&lt;br /&gt;
**Summary: &lt;br /&gt;
**Pro: &lt;br /&gt;
**Con: &lt;br /&gt;
*[http://robots.thoughtbot.com/post/14825364877/evaluating-alternative-decorator-implementations-in Evaluating alternative Decorator implementations in Ruby]&lt;br /&gt;
**Summary: &lt;br /&gt;
**Pro: &lt;br /&gt;
**Con: &lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking - Decorator Design Pattern]&lt;br /&gt;
**Summary: Window example with Java snippets, checklist and rules of thumb&lt;br /&gt;
**Pro: This page presents an interesting concrete example with UML diagrams and code snippets. At the end, it covers a checklist for using the Decorator pattern as well as some rules of thumb.&lt;br /&gt;
**Con: The code provided in this resource are just snippets, not a full example.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Fwww.agiledeveloper.com%2Farticles%2FDecoratorByExample.pdf Decorator Pattern by Example]&lt;br /&gt;
**Summary: &lt;br /&gt;
**Pro: &lt;br /&gt;
**Con: &lt;br /&gt;
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ Design Pattern Series]&lt;br /&gt;
**Summary: &lt;br /&gt;
**Pro: &lt;br /&gt;
**Con: &lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
**Summary: Code intensive GUI example in Java&lt;br /&gt;
**Pro: The UML diagram is nice and the code is quite complete. The concrete example is applicable to the real world.&lt;br /&gt;
**Con: The page does not provide too much explanation, mainly just a code sample.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69611</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69611"/>
		<updated>2012-11-16T21:20:49Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Directory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Overview of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx - 2001 - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/ - 2011&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Understand the UML diagram for the Decorator pattern can help you understand the purpose of the Decorator pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.&lt;br /&gt;
&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004 - 2008&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For a general overview and UML diagrams of the Decorator design pattern, we recommend:&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia]&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory]&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers]&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
Understanding the general concepts behind using the Decorator pattern would be the first step for any language implementation. The next step is usually finding examples that show how the pattern can be used, possibly by language. Here we will look at sources that give useful examples for understanding how to implement the Decorator pattern in Ruby. It is also worth noting that all references cited in this wiki are useful for understanding the pattern in general, despite the language that may be used in the examples.&lt;br /&gt;
&lt;br /&gt;
The most popular example used to explain the Decorator pattern in Ruby is an example involving a Coffee class, of which there are various subclasses of coffee that can have various additional ingredients (milk, sugar, etc.). For many of the sites, they mentioned using the &amp;quot;Head First Design Patterns&amp;quot; book as a source, so of course we recommend the same &amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;. The biggest disadvantage to using this resource is that it is a part of a textbook so is rather lengthy. There are a variety of sites devoted to explaining this pattern (most of which use the Coffee example) but a few stood out as especially useful in various ways. One of the better resources for any project is someone that has used the pattern before and blogs are a great place to start. One blog in particular gives a great explanation of the Decorator pattern being used in the Coffee example &amp;lt;ref name=&amp;quot;8lines&amp;quot;&amp;gt; http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html - 2006&amp;lt;/ref&amp;gt;. This is one of the sources that explicitly mentioned using the &amp;quot;Head First Design Patterns&amp;quot; book, so it may be easy to use these resources in parallel.&lt;br /&gt;
&lt;br /&gt;
Another useful&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt;; in this book an in-depth explanation of the Decorator Pattern can be found. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.&lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional developers&amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012?&amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern as well as textual explanations if needed. An advantage to using this resource is that it uses less text and more examples to show how the Decorator pattern can be used. It is also helpful that this site has multiple examples that can help increase understanding through reinforcement and multiple viewpoints (and languages). A disadvantage to using this site is that all of the examples are general examples using the pattern as opposed to real life situations or scenarios (like the Coffee example). If it is more useful for you to have a more in-depth walk through of a general example, then using the &amp;quot;Decorator Pattern by Example&amp;quot; page may be more helpful &amp;lt;ref name=&amp;quot;dpexample&amp;quot;&amp;gt; http://www.agiledeveloper.com/articles/DecoratorByExample.pdf - ? &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some sites use over-arching real life examples to explain the Decorator pattern and its usage. The most popular of these examples (in any language) is the Coffee class mentioned previously, however there are other examples based on real life scenarios and situations that can relay the same information. One that seems to be useful in conveying the idea behind the Decorator pattern is using the pattern to report sports statistics for a given team. One site uses the example of a developer hired to create software that reports Soccer sports statistics &amp;lt;ref name=&amp;quot;soccer&amp;quot;&amp;gt;http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ - 2012 &amp;lt;/ref&amp;gt;. This site uses a mix of textual explanations and examples to explain the pattern. One advantage to using this site is that it is relatively short and includes a real world example. It also provides the code in a way that would make it simple to attempt to run it yourself and see how it works. A disadvantage for this resource is that it does not break down exactly how it got all of its code so it may be difficult to understand how to get going on your own using your own &amp;quot;problem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Although most examples for this pattern focus on other usages, it is possible to use the Decorator pattern with GUIs (Graphical User Interfaces). A useful example for understanding this usage can be found on OODesign.com &amp;lt;ref name=&amp;quot;gui&amp;quot;&amp;gt;http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html - ?&amp;lt;/ref&amp;gt;. This site uses code and UML diagrams to explain the usage of Decorator when implementing GUIs. There are small textual explanations in between code snippets but the bulk of the page is code. This site is useful because it offers code that can easily be tested and/or modified if needed to gain a better understanding. Another advantage is that there is not much that has to be read. A disadvantage is that if the code given is not similar to what you are trying to achieve it may be difficult to understand whether you need to be using the Decorator pattern for you particular GUI; outside sources may be needed for this understanding.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For Java and C# resources, we recommend:&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking]&lt;br /&gt;
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ blog.image0]&lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Summary: Typical Wiki page&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory - Decorator Design Pattern]&lt;br /&gt;
**Summary: Quick reference of UML and participants&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern]&lt;br /&gt;
**Summary: Ice Cream example in Java&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Summary: Textbook chapter&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
*[http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html confessions of a ruby programmer]&lt;br /&gt;
**Summary: &lt;br /&gt;
**Pro: &lt;br /&gt;
**Con: &lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking - Decorator Design Pattern]&lt;br /&gt;
**Summary: Window example with Java snippets, checklist and rules of thumb&lt;br /&gt;
**Pro: This page presents an interesting concrete example with UML diagrams and code snippets. At the end, it covers a checklist for using the Decorator pattern as well as some rules of thumb.&lt;br /&gt;
**Con: The code provided in this resource are just snippets, not a full example.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Fwww.agiledeveloper.com%2Farticles%2FDecoratorByExample.pdf Decorator Pattern by Example]&lt;br /&gt;
**Summary: &lt;br /&gt;
**Pro: &lt;br /&gt;
**Con: &lt;br /&gt;
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ Design Pattern Series]&lt;br /&gt;
**Summary: &lt;br /&gt;
**Pro: &lt;br /&gt;
**Con: &lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
**Summary: Code intensive GUI example in Java&lt;br /&gt;
**Pro: The UML diagram is nice and the code is quite complete. The concrete example is applicable to the real world.&lt;br /&gt;
**Con: The page does not provide too much explanation, mainly just a code sample.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69610</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69610"/>
		<updated>2012-11-16T21:16:30Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Directory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Overview of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx - 2001 - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/ - 2011&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Understand the UML diagram for the Decorator pattern can help you understand the purpose of the Decorator pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.&lt;br /&gt;
&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004 - 2008&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For a general overview and UML diagrams of the Decorator design pattern, we recommend:&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia]&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory]&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers]&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
Understanding the general concepts behind using the Decorator pattern would be the first step for any language implementation. The next step is usually finding examples that show how the pattern can be used, possibly by language. Here we will look at sources that give useful examples for understanding how to implement the Decorator pattern in Ruby. It is also worth noting that all references cited in this wiki are useful for understanding the pattern in general, despite the language that may be used in the examples.&lt;br /&gt;
&lt;br /&gt;
The most popular example used to explain the Decorator pattern in Ruby is an example involving a Coffee class, of which there are various subclasses of coffee that can have various additional ingredients (milk, sugar, etc.). For many of the sites, they mentioned using the &amp;quot;Head First Design Patterns&amp;quot; book as a source, so of course we recommend the same &amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;. The biggest disadvantage to using this resource is that it is a part of a textbook so is rather lengthy. There are a variety of sites devoted to explaining this pattern (most of which use the Coffee example) but a few stood out as especially useful in various ways. One of the better resources for any project is someone that has used the pattern before and blogs are a great place to start. One blog in particular gives a great explanation of the Decorator pattern being used in the Coffee example &amp;lt;ref name=&amp;quot;8lines&amp;quot;&amp;gt; http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html - 2006&amp;lt;/ref&amp;gt;. This is one of the sources that explicitly mentioned using the &amp;quot;Head First Design Patterns&amp;quot; book, so it may be easy to use these resources in parallel.&lt;br /&gt;
&lt;br /&gt;
Another useful&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt;; in this book an in-depth explanation of the Decorator Pattern can be found. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.&lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional developers&amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012?&amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern as well as textual explanations if needed. An advantage to using this resource is that it uses less text and more examples to show how the Decorator pattern can be used. It is also helpful that this site has multiple examples that can help increase understanding through reinforcement and multiple viewpoints (and languages). A disadvantage to using this site is that all of the examples are general examples using the pattern as opposed to real life situations or scenarios (like the Coffee example). If it is more useful for you to have a more in-depth walk through of a general example, then using the &amp;quot;Decorator Pattern by Example&amp;quot; page may be more helpful &amp;lt;ref name=&amp;quot;dpexample&amp;quot;&amp;gt; http://www.agiledeveloper.com/articles/DecoratorByExample.pdf - ? &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some sites use over-arching real life examples to explain the Decorator pattern and its usage. The most popular of these examples (in any language) is the Coffee class mentioned previously, however there are other examples based on real life scenarios and situations that can relay the same information. One that seems to be useful in conveying the idea behind the Decorator pattern is using the pattern to report sports statistics for a given team. One site uses the example of a developer hired to create software that reports Soccer sports statistics &amp;lt;ref name=&amp;quot;soccer&amp;quot;&amp;gt;http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ - 2012 &amp;lt;/ref&amp;gt;. This site uses a mix of textual explanations and examples to explain the pattern. One advantage to using this site is that it is relatively short and includes a real world example. It also provides the code in a way that would make it simple to attempt to run it yourself and see how it works. A disadvantage for this resource is that it does not break down exactly how it got all of its code so it may be difficult to understand how to get going on your own using your own &amp;quot;problem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Although most examples for this pattern focus on other usages, it is possible to use the Decorator pattern with GUIs (Graphical User Interfaces). A useful example for understanding this usage can be found on OODesign.com &amp;lt;ref name=&amp;quot;gui&amp;quot;&amp;gt;http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html - ?&amp;lt;/ref&amp;gt;. This site uses code and UML diagrams to explain the usage of Decorator when implementing GUIs. There are small textual explanations in between code snippets but the bulk of the page is code. This site is useful because it offers code that can easily be tested and/or modified if needed to gain a better understanding. Another advantage is that there is not much that has to be read. A disadvantage is that if the code given is not similar to what you are trying to achieve it may be difficult to understand whether you need to be using the Decorator pattern for you particular GUI; outside sources may be needed for this understanding.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For Java and C# resources, we recommend:&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking]&lt;br /&gt;
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ blog.image0]&lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Summary: Typical Wiki page&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory - Decorator Design Pattern]&lt;br /&gt;
**Summary: Quick reference of UML and participants&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern]&lt;br /&gt;
**Summary: Ice Cream example in Java&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Summary: Textbook chapter&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking - Decorator Design Pattern]&lt;br /&gt;
**Summary: Window example with Java snippets, checklist and rules of thumb&lt;br /&gt;
**Pro: This page presents an interesting concrete example with UML diagrams and code snippets. At the end, it covers a checklist for using the Decorator pattern as well as some rules of thumb.&lt;br /&gt;
**Con: The code provided in this resource are just snippets, not a full example.&lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
**Summary: Code intensive GUI example in Java&lt;br /&gt;
**Pro: The UML diagram is nice and the code is quite complete. The concrete example is applicable to the real world.&lt;br /&gt;
**Con: The page does not provide too much explanation, mainly just a code sample.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69605</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69605"/>
		<updated>2012-11-16T21:01:30Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Directory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Overview of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx - 2001 - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/ - 2011&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Understand the UML diagram for the Decorator pattern can help you understand the purpose of the Decorator pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.&lt;br /&gt;
&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004 - 2008&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For a general overview and UML diagrams of the Decorator design pattern, we recommend:&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia]&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory]&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers]&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt;; in this book an in-depth explanation of the Decorator Pattern can be found. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.&lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional developers&amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012?&amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern as well as textual explanations if needed. An advantage to using this resource is that it uses less text and more examples to show how the Decorator pattern can be used. It is also helpful that this site has multiple examples that can help increase understanding through reinforcement and multiple viewpoints (and languages). A disadvantage to using this site is that all of the examples are general examples using the pattern as opposed to real life situations or scenarios (like the Coffee example). If it is more useful for you to have a more in-depth walk through of a general example, then using the &amp;quot;Decorator Pattern by Example&amp;quot; page may be more helpful &amp;lt;ref name=&amp;quot;dpexample&amp;quot;&amp;gt; http://www.agiledeveloper.com/articles/DecoratorByExample.pdf - ? &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some sites use over-arching real life examples to explain the Decorator pattern and its usage. The most popular of these examples (in any language) is the Coffee class mentioned previously, however there are other examples based on real life scenarios and situations that can relay the same information. One that seems to be useful in conveying the idea behind the Decorator pattern is using the pattern to report sports statistics for a given team. One site uses the example of a developer hired to create software that reports Soccer sports statistics &amp;lt;ref name=&amp;quot;soccer&amp;quot;&amp;gt;http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ - 2012 &amp;lt;/ref&amp;gt;. This site uses a mix of textual explanations and examples to explain the pattern. One advantage to using this site is that it is relatively short and includes a real world example. It also provides the code in a way that would make it simple to attempt to run it yourself and see how it works. A disadvantage for this resource is that it does not break down exactly how it got all of its code so it may be difficult to understand how to get going on your own using your own &amp;quot;problem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Although most examples for this pattern focus on other usages, it is possible to use the Decorator pattern with GUIs (Graphical User Interfaces). A useful example for understanding this usage can be found on OODesign.com &amp;lt;ref name=&amp;quot;gui&amp;quot;&amp;gt;http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html - ?&amp;lt;/ref&amp;gt;. This site uses code and UML diagrams to explain the usage of Decorator when implementing GUIs. There are small textual explanations in between code snippets but the bulk of the page is code. This site is useful because it offers code that can easily be tested and/or modified if needed to gain a better understanding. Another advantage is that there is not much that has to be read. A disadvantage is that if the code given is not similar to what you are trying to achieve it may be difficult to understand whether you need to be using the Decorator pattern for you particular GUI; outside sources may be needed for this understanding.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For Java and C# resources, we recommend:&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking]&lt;br /&gt;
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ blog.image0]&lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.google.com/url?q=http%3A%2F%2Fwww.dofactory.com%2FPatterns%2FPatternDecorator.aspx&amp;amp;sa=D&amp;amp;sntz=1&amp;amp;usg=AFQjCNGFh9wp9H2FSxI5drUZfg4YE-Au3g dofactory - Decorator Design Pattern]&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern (Java)]&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking - Decorator Design Pattern]&lt;br /&gt;
**Pro: This page presents an interesting concrete example with UML diagrams and code snippets. At the end, it covers a checklist for using the Decorator pattern as well as some rules of thumb.&lt;br /&gt;
**Con: The code provided in this resource are just snippets, not a full example.&lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
**Pro: The UML diagram is nice and the code is quite complete. The concrete example is applicable to the real world.&lt;br /&gt;
**Con: The page does not provide too much explanation, mainly just a code sample.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69602</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69602"/>
		<updated>2012-11-16T20:59:21Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* General Resources */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Overview of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx - 2001 - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/ - 2011&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Understand the UML diagram for the Decorator pattern can help you understand the purpose of the Decorator pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.&lt;br /&gt;
&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004 - 2008&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For a general overview and UML diagrams of the Decorator design pattern, we recommend:&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia]&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory]&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers]&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt;; in this book an in-depth explanation of the Decorator Pattern can be found. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.&lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional developers&amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012?&amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern as well as textual explanations if needed. An advantage to using this resource is that it uses less text and more examples to show how the Decorator pattern can be used. It is also helpful that this site has multiple examples that can help increase understanding through reinforcement and multiple viewpoints (and languages). A disadvantage to using this site is that all of the examples are general examples using the pattern as opposed to real life situations or scenarios (like the Coffee example).&lt;br /&gt;
&lt;br /&gt;
Some sites use over-arching real life examples to explain the Decorator pattern and its usage. The most popular of these examples (in any language) is the Coffee class mentioned previously, however there are other examples based on real life scenarios and situations that can relay the same information. One that seems to be useful in conveying the idea behind the Decorator pattern is using the pattern to report sports statistics for a given team. One site uses the example of a developer hired to create software that reports Soccer sports statistics &amp;lt;ref name=&amp;quot;soccer&amp;quot;&amp;gt;http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ - 2012 &amp;lt;/ref&amp;gt;. This site uses a mix of textual explanations and examples to explain the pattern. One advantage to using this site is that it is relatively short and includes a real world example. It also provides the code in a way that would make it simple to attempt to run it yourself and see how it works. A disadvantage for this resource is that it does not break down exactly how it got all of its code so it may be difficult to understand how to get going on your own using your own &amp;quot;problem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Although most examples for this pattern focus on other usages, it is possible to use the Decorator pattern with GUIs (Graphical User Interfaces). A useful example for understanding this usage can be found on OODesign.com &amp;lt;ref name=&amp;quot;gui&amp;quot;&amp;gt;http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html - ?&amp;lt;/ref&amp;gt;. This site uses code and UML diagrams to explain the usage of Decorator when implementing GUIs. There are small textual explanations in between code snippets but the bulk of the page is code. This site is useful because it offers code that can easily be tested and/or modified if needed to gain a better understanding. Another advantage is that there is not much that has to be read. A disadvantage is that if the code given is not similar to what you are trying to achieve it may be difficult to understand whether you need to be using the Decorator pattern for you particular GUI; outside sources may be needed for this understanding.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For Java and C# resources, we recommend:&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking]&lt;br /&gt;
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ blog.image0]&lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.google.com/url?q=http%3A%2F%2Fwww.dofactory.com%2FPatterns%2FPatternDecorator.aspx&amp;amp;sa=D&amp;amp;sntz=1&amp;amp;usg=AFQjCNGFh9wp9H2FSxI5drUZfg4YE-Au3g dofactory - Decorator Design Pattern]&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern (Java)]&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking - Decorator Design Pattern]&lt;br /&gt;
**Pro: This page presents an interesting concrete example with UML diagrams and code snippets. At the end, it covers a checklist for using the Decorator pattern as well as some rules of thumb.&lt;br /&gt;
**Con: The code provided in this resource are just snippets, not a full example.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69601</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69601"/>
		<updated>2012-11-16T20:58:45Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Java &amp;amp; C# */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Description of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx - 2001 - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/ - 2011&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Understand the UML diagram for the Decorator pattern can help you understand the purpose of the Decorator pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.&lt;br /&gt;
&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004 - 2008&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
The resources we recommend for this section are:&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia]&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory]&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers]&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt;; in this book an in-depth explanation of the Decorator Pattern can be found. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.&lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot;&amp;lt;ref name=&amp;quot;head_first&amp;quot;/&amp;gt; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional developers&amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012?&amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern as well as textual explanations if needed. An advantage to using this resource is that it uses less text and more examples to show how the Decorator pattern can be used. It is also helpful that this site has multiple examples that can help increase understanding through reinforcement and multiple viewpoints (and languages). A disadvantage to using this site is that all of the examples are general examples using the pattern as opposed to real life situations or scenarios (like the Coffee example).&lt;br /&gt;
&lt;br /&gt;
Some sites use over-arching real life examples to explain the Decorator pattern and its usage. The most popular of these examples (in any language) is the Coffee class mentioned previously, however there are other examples based on real life scenarios and situations that can relay the same information. One that seems to be useful in conveying the idea behind the Decorator pattern is using the pattern to report sports statistics for a given team. One site uses the example of a developer hired to create software that reports Soccer sports statistics &amp;lt;ref name=&amp;quot;soccer&amp;quot;&amp;gt;http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ - 2012 &amp;lt;/ref&amp;gt;. This site uses a mix of textual explanations and examples to explain the pattern. One advantage to using this site is that it is relatively short and includes a real world example. It also provides the code in a way that would make it simple to attempt to run it yourself and see how it works. A disadvantage for this resource is that it does not break down exactly how it got all of its code so it may be difficult to understand how to get going on your own using your own &amp;quot;problem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Although most examples for this pattern focus on other usages, it is possible to use the Decorator pattern with GUIs (Graphical User Interfaces). A useful example for understanding this usage can be found on OODesign.com &amp;lt;ref name=&amp;quot;gui&amp;quot;&amp;gt;http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html - ?&amp;lt;/ref&amp;gt;. This site uses code and UML diagrams to explain the usage of Decorator when implementing GUIs. There are small textual explanations in between code snippets but the bulk of the page is code. This site is useful because it offers code that can easily be tested and/or modified if needed to gain a better understanding. Another advantage is that there is not much that has to be read. A disadvantage is that if the code given is not similar to what you are trying to achieve it may be difficult to understand whether you need to be using the Decorator pattern for you particular GUI; outside sources may be needed for this understanding.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
For Java and C# resources, we recommend:&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking]&lt;br /&gt;
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ blog.image0]&lt;br /&gt;
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.google.com/url?q=http%3A%2F%2Fwww.dofactory.com%2FPatterns%2FPatternDecorator.aspx&amp;amp;sa=D&amp;amp;sntz=1&amp;amp;usg=AFQjCNGFh9wp9H2FSxI5drUZfg4YE-Au3g dofactory - Decorator Design Pattern]&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern (Java)]&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking - Decorator Design Pattern]&lt;br /&gt;
**Pro: This page presents an interesting concrete example with UML diagrams and code snippets. At the end, it covers a checklist for using the Decorator pattern as well as some rules of thumb.&lt;br /&gt;
**Con: The code provided in this resource are just snippets, not a full example.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69597</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69597"/>
		<updated>2012-11-16T20:29:10Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Directory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Description of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx - 2001 - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/ - 2011&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Understand the UML diagram for the Decorator pattern can help you understand the purpose of the Decorator pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.&lt;br /&gt;
&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004 - 2008&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
The resources we recommend for this section are:&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia]&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory]&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers]&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;; in this book an in-depth explanation of the Decorator Pattern can be found &amp;lt;ref name=&amp;quot;headfirst&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004&amp;lt;/ref&amp;gt;. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.  &lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional  developers &amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012? &amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern.&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.google.com/url?q=http%3A%2F%2Fwww.dofactory.com%2FPatterns%2FPatternDecorator.aspx&amp;amp;sa=D&amp;amp;sntz=1&amp;amp;usg=AFQjCNGFh9wp9H2FSxI5drUZfg4YE-Au3g dofactory - Decorator Design Pattern]&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern (Java)]&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking - Decorator Design Pattern]&lt;br /&gt;
**Pro: This page presents an interesting concrete example with UML diagrams and code snippets. At the end, it covers a checklist for using the Decorator pattern as well as some rules of thumb.&lt;br /&gt;
**Con: The code provided in this resource are just snippets, not a full example.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69596</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69596"/>
		<updated>2012-11-16T20:24:44Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Directory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Description of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx - 2001 - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/ - 2011&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Understand the UML diagram for the Decorator pattern can help you understand the purpose of the Decorator pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.&lt;br /&gt;
&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004 - 2008&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
The resources we recommend for this section are:&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia]&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory]&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers]&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;; in this book an in-depth explanation of the Decorator Pattern can be found &amp;lt;ref name=&amp;quot;headfirst&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004&amp;lt;/ref&amp;gt;. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.  &lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional  developers &amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012? &amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern.&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.google.com/url?q=http%3A%2F%2Fwww.dofactory.com%2FPatterns%2FPatternDecorator.aspx&amp;amp;sa=D&amp;amp;sntz=1&amp;amp;usg=AFQjCNGFh9wp9H2FSxI5drUZfg4YE-Au3g dofactory - Decorator Design Pattern]&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern (Java)]&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/decorator SourceMaking - Decorator Design Pattern]&lt;br /&gt;
**Pro:&lt;br /&gt;
**Con:&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69595</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69595"/>
		<updated>2012-11-16T20:22:22Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* General Resources */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Description of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx - 2001 - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/ - 2011&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Understand the UML diagram for the Decorator pattern can help you understand the purpose of the Decorator pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.&lt;br /&gt;
&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004 - 2008&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
The resources we recommend for this section are:&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia]&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory]&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers]&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;; in this book an in-depth explanation of the Decorator Pattern can be found &amp;lt;ref name=&amp;quot;headfirst&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004&amp;lt;/ref&amp;gt;. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.  &lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional  developers &amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012? &amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern.&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.google.com/url?q=http%3A%2F%2Fwww.dofactory.com%2FPatterns%2FPatternDecorator.aspx&amp;amp;sa=D&amp;amp;sntz=1&amp;amp;usg=AFQjCNGFh9wp9H2FSxI5drUZfg4YE-Au3g dofactory - Decorator Design Pattern]&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern (Java)]&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69594</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69594"/>
		<updated>2012-11-16T20:18:55Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* General Resources */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Description of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Understand the UML diagram for the Decorator pattern can help you understand the purpose of the Decorator pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.&lt;br /&gt;
&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
The resources we recommend for this section are:&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia]&lt;br /&gt;
*[http://www.dofactory.com/Patterns/PatternDecorator.aspx dofactory]&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers]&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;; in this book an in-depth explanation of the Decorator Pattern can be found &amp;lt;ref name=&amp;quot;headfirst&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004&amp;lt;/ref&amp;gt;. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.  &lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional  developers &amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012? &amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern.&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.google.com/url?q=http%3A%2F%2Fwww.dofactory.com%2FPatterns%2FPatternDecorator.aspx&amp;amp;sa=D&amp;amp;sntz=1&amp;amp;usg=AFQjCNGFh9wp9H2FSxI5drUZfg4YE-Au3g dofactory - Decorator Design Pattern]&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern (Java)]&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69593</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69593"/>
		<updated>2012-11-16T20:14:32Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* UML */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Description of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Understand the UML diagram for the Decorator pattern can help you understand the purpose of the Decorator pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.&lt;br /&gt;
&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;; in this book an in-depth explanation of the Decorator Pattern can be found &amp;lt;ref name=&amp;quot;headfirst&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004&amp;lt;/ref&amp;gt;. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.  &lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional  developers &amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012? &amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern.&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.google.com/url?q=http%3A%2F%2Fwww.dofactory.com%2FPatterns%2FPatternDecorator.aspx&amp;amp;sa=D&amp;amp;sntz=1&amp;amp;usg=AFQjCNGFh9wp9H2FSxI5drUZfg4YE-Au3g dofactory - Decorator Design Pattern]&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern (Java)]&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69592</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69592"/>
		<updated>2012-11-16T20:11:25Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Description of the Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Description of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and discusses use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
There are a variety of resources available for learning how to use the Decorator pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book &amp;quot;Head First Design Patterns&amp;quot;; in this book an in-depth explanation of the Decorator Pattern can be found &amp;lt;ref name=&amp;quot;headfirst&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf - 2004&amp;lt;/ref&amp;gt;. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example. It is also worth noting the the coding example used in this book (and a number of resources dedicated to the Decorator pattern) is a Coffee class where you wish to &amp;quot;decorate&amp;quot; your coffee with different additional ingredients. If you are really interested in learning the ins and outs of the Decorator pattern and do not mind a lengthy read, this is the best reference to use in regard to learning and understanding how to use this pattern.  &lt;br /&gt;
&lt;br /&gt;
If you are interested in learning about the Decorator pattern for Java and/or C# but do not have enough time or energy to read the chapter in the &amp;quot;Head First Design Patterns&amp;quot; book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional  developers &amp;lt;ref name=&amp;quot;sourcemaking&amp;quot;&amp;gt; http://sourcemaking.com/design_patterns/decorator - 2012? &amp;lt;/ref&amp;gt;. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern.&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.google.com/url?q=http%3A%2F%2Fwww.dofactory.com%2FPatterns%2FPatternDecorator.aspx&amp;amp;sa=D&amp;amp;sntz=1&amp;amp;usg=AFQjCNGFh9wp9H2FSxI5drUZfg4YE-Au3g dofactory - Decorator Design Pattern]&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern (Java)]&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69590</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69590"/>
		<updated>2012-11-16T20:10:06Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Decorator Directory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Description of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Decorator_pattern&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.dofactory.com/Patterns/PatternDecorator.aspx&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and talks use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.google.com/url?q=http%3A%2F%2Fwww.dofactory.com%2FPatterns%2FPatternDecorator.aspx&amp;amp;sa=D&amp;amp;sntz=1&amp;amp;usg=AFQjCNGFh9wp9H2FSxI5drUZfg4YE-Au3g dofactory - Decorator Design Pattern]&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern (Java)]&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69589</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69589"/>
		<updated>2012-11-16T20:08:40Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Description of the Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Description of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and talks use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or a Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
&lt;br /&gt;
===UML===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.google.com/url?q=http%3A%2F%2Fwww.dofactory.com%2FPatterns%2FPatternDecorator.aspx&amp;amp;sa=D&amp;amp;sntz=1&amp;amp;usg=AFQjCNGFh9wp9H2FSxI5drUZfg4YE-Au3g dofactory - Decorator Design Pattern]&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern (Java)]&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69588</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69588"/>
		<updated>2012-11-16T20:07:52Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* General Resources */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Description of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and talks use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
If you understand Java (or Java like language), javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/&amp;lt;/ref&amp;gt; is a good page that gives a basic overview of the pattern through the presentation of a concrete example. There are other Java resources and resources for other languages that are similar below.&lt;br /&gt;
===UML===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;/&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.google.com/url?q=http%3A%2F%2Fwww.dofactory.com%2FPatterns%2FPatternDecorator.aspx&amp;amp;sa=D&amp;amp;sntz=1&amp;amp;usg=AFQjCNGFh9wp9H2FSxI5drUZfg4YE-Au3g dofactory - Decorator Design Pattern]&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern (Java)]&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69587</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69587"/>
		<updated>2012-11-16T20:04:39Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Description of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and talks use of the patterns in several languages at a high level.&lt;br /&gt;
===UML===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/&amp;lt;/ref&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
==Directory==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Decorator_pattern Wikipedia - Decorator Pattern]&lt;br /&gt;
**Pro: Provides background, motivation and a good overview of the pattern, as well as some high level examples.&lt;br /&gt;
**Con: Wikipedia can be fairly difficult to follow sometimes and this article is not much of an exception (also, Wikipedia's credibility is debated).&lt;br /&gt;
*[http://www.google.com/url?q=http%3A%2F%2Fwww.dofactory.com%2FPatterns%2FPatternDecorator.aspx&amp;amp;sa=D&amp;amp;sntz=1&amp;amp;usg=AFQjCNGFh9wp9H2FSxI5drUZfg4YE-Au3g dofactory - Decorator Design Pattern]&lt;br /&gt;
**Pro: Provides a quick definition, list of participants and abstract UML diagram.&lt;br /&gt;
**Con: Not much of an explanation - just good for referencing.&lt;br /&gt;
*[http://javapapers.com/design-patterns/decorator-pattern/ javapapers - Decorator Design Pattern (Java)]&lt;br /&gt;
**Pro: Provides a short overview of the pattern as well as a good concrete example with UML and Java code.&lt;br /&gt;
**Con: The concrete example is a good start, but it may not be real-world enough to provide a complete understand.&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]&lt;br /&gt;
**Pro: Is a excerpt from a commercial textbook, so the source is extremely credible and provides an in depth explanation of the design pattern complete with UML and a concrete example.&lt;br /&gt;
**Con: The textbook does not appear too professional and provides a lot of background that isn't really necessary for understanding the pattern. This is very long read.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69586</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69586"/>
		<updated>2012-11-16T19:53:04Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* General Resources */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Description of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and talks use of the patterns in several languages at a high level.&lt;br /&gt;
===UML===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; both provide UML diagrams of the Decorator Pattern concept. For UML diagrams of concrete examples, javapapers&amp;lt;ref name=&amp;quot;javapapers&amp;quot;&amp;gt;http://javapapers.com/design-patterns/decorator-pattern/&amp;lt;/ref&amp;gt; provides an Ice Cream example and Head First Design Patterns&amp;lt;ref name=&amp;quot;head_first&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf&amp;lt;/ref&amp;gt; provides a couple as well, but it is a very long read. Many of the language specific resources listed later in this article also provide UML diagrams for their concrete examples/implementations.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69585</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69585"/>
		<updated>2012-11-16T19:49:04Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Description of the Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Description of the Pattern===&lt;br /&gt;
Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; and dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; provide solid explanations of the Decorator Pattern. dofactory&amp;lt;ref name=&amp;quot;dofactory&amp;quot;/&amp;gt; is much more concise than Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia&amp;lt;ref name=&amp;quot;wikipedia&amp;quot;/&amp;gt;, on the other hand, is much more verbose, but goes into the motivation for the pattern and talks use of the patterns in several languages at a high level.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69584</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69584"/>
		<updated>2012-11-16T19:48:51Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
===Description of the Pattern===&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69583</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w59 bc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w59_bc&amp;diff=69583"/>
		<updated>2012-11-16T19:39:07Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: Created page with &amp;quot;=Decorator Directory=  ==General Resources==  ==Ruby==  ==Java &amp;amp; C#==&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Decorator Directory=&lt;br /&gt;
&lt;br /&gt;
==General Resources==&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Java &amp;amp; C#==&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=69582</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=69582"/>
		<updated>2012-11-16T19:34:19Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: &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;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67519</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67519"/>
		<updated>2012-10-11T03:28:04Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Quantifier */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref name=&amp;quot;tutorialspoint&amp;quot;&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm - 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref name=&amp;quot;tutorialspoint&amp;quot;/&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref name=&amp;quot;saas&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// List defined above&lt;br /&gt;
listIterator litr = l.listIterator(); &lt;br /&gt;
while(litr.hasNext()) {&lt;br /&gt;
    Object element = litr.next(); &lt;br /&gt;
    element.foo();&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# List defined above&lt;br /&gt;
list.each { |x| x.foo() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref name=&amp;quot;csc517exam&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf - 2008&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref name=&amp;quot;saas_pastebin&amp;quot;&amp;gt;http://pastebin.com/T3JhV7Bk - 2012&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref name=&amp;quot;saas&amp;quot;/&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
====Quantifier====&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec5&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Flectures%2Fnotes%2Fol5.pdf - 2010&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Quantifier&lt;br /&gt;
  def any?&lt;br /&gt;
    each {|x| return true if yield x}&lt;br /&gt;
    false&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def all?&lt;br /&gt;
    each {|x| return false if not yield x}&lt;br /&gt;
    true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Array&lt;br /&gt;
  include Quantifier&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts [1, 2, 3].any? {|x| x == 5}&lt;br /&gt;
# false&lt;br /&gt;
puts [4, 3, 4].all? {|x| x == 4}&lt;br /&gt;
# false&lt;br /&gt;
&lt;br /&gt;
puts [1, 2, 3].any? {|x| x == 3}&lt;br /&gt;
#true&lt;br /&gt;
puts [4, 4, 4].all? {|x| x == 4}&lt;br /&gt;
#true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, covered in the Lecture 5 slides, we combine our knowledge of how iterators work with &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt;, and create our own versions of these iterators. The module &amp;lt;code&amp;gt;Quantifier&amp;lt;/code&amp;gt; is created, that allows a collection (such as &amp;lt;code&amp;gt;Array&amp;lt;/code&amp;gt;), to test whether any or all of a set of properties are held true. By examining &amp;lt;code&amp;gt;any&amp;lt;/code&amp;gt;, you can see that for every element in the collection, you the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function is called and it return true if a single element returns true. The &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt; method returns false, if any &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; call returns false. This extension of each using &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; demonstrates how extensible &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is for iterating across elements.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
As has been demonstrated in the previous examples and explanations, &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; allows programmers to pass instructions to objects in Ruby that can be called at any time as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson] - 2012&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow] - 2010&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow] - 2010&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint] - 2012&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Flectures%2Fnotes%2Fol5.pdf Class Notes CSC517 - Lecture 5] - 2010&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67518</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67518"/>
		<updated>2012-10-11T03:23:22Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Quantifier */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref name=&amp;quot;tutorialspoint&amp;quot;&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm - 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref name=&amp;quot;tutorialspoint&amp;quot;/&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref name=&amp;quot;saas&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// List defined above&lt;br /&gt;
listIterator litr = l.listIterator(); &lt;br /&gt;
while(litr.hasNext()) {&lt;br /&gt;
    Object element = litr.next(); &lt;br /&gt;
    element.foo();&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# List defined above&lt;br /&gt;
list.each { |x| x.foo() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref name=&amp;quot;csc517exam&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf - 2008&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref name=&amp;quot;saas_pastebin&amp;quot;&amp;gt;http://pastebin.com/T3JhV7Bk - 2012&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref name=&amp;quot;saas&amp;quot;/&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
====Quantifier====&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec5&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Flectures%2Fnotes%2Fol5.pdf - 2010&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Quantifier&lt;br /&gt;
  def any?&lt;br /&gt;
    each {|x| return true if yield x}&lt;br /&gt;
    false&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def all?&lt;br /&gt;
    each {|x| return false if not yield x}&lt;br /&gt;
    true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Array&lt;br /&gt;
  include Quantifier&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts [1, 2, 3].any? {|x| x == 5}&lt;br /&gt;
# false&lt;br /&gt;
puts [4, 3, 4].all? {|x| x == 4}&lt;br /&gt;
# false&lt;br /&gt;
&lt;br /&gt;
puts [1, 2, 3].any? {|x| x == 3}&lt;br /&gt;
#true&lt;br /&gt;
puts [4, 4, 4].all? {|x| x == 4}&lt;br /&gt;
#true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, covered in the Lecture 5 slides, we combine our knowledge of how iterators work with &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt;, and create our own versions of these iterators. The module &amp;lt;code&amp;gt;Quantifier&amp;lt;/code&amp;gt; is created, that allows a collection(such as &amp;lt;code&amp;gt;Array&amp;lt;/code&amp;gt;), to test whether any or all of a set of properties are held true. By examining any, you can see that for every element, you test the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement and return true if a single element returns true. Whereas for the all statement, if any &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement returns false, then the answer will be false. This extension of each using &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; demonstrates how extensible &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is for iterating across elements.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
As has been demonstrated in the previous examples and explanations, &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; allows programmers to pass instructions to objects in Ruby that can be called at any time as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson] - 2012&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow] - 2010&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow] - 2010&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint] - 2012&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Flectures%2Fnotes%2Fol5.pdf Class Notes CSC517 - Lecture 5] - 2010&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67517</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67517"/>
		<updated>2012-10-11T03:15:33Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* SaaS - 3.8 yield() */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref name=&amp;quot;tutorialspoint&amp;quot;&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm - 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref name=&amp;quot;tutorialspoint&amp;quot;/&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref name=&amp;quot;saas&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// List defined above&lt;br /&gt;
listIterator litr = l.listIterator(); &lt;br /&gt;
while(litr.hasNext()) {&lt;br /&gt;
    Object element = litr.next(); &lt;br /&gt;
    element.foo();&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# List defined above&lt;br /&gt;
list.each { |x| x.foo() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref name=&amp;quot;csc517exam&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf - 2008&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref name=&amp;quot;saas_pastebin&amp;quot;&amp;gt;http://pastebin.com/T3JhV7Bk - 2012&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref name=&amp;quot;saas&amp;quot;/&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
====Quantifier====&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec5&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Flectures%2Fnotes%2Fol5.pdf - 2010&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Quantifier&lt;br /&gt;
  def any?&lt;br /&gt;
  each {|x| return true if yield x}&lt;br /&gt;
    false&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def all?&lt;br /&gt;
each {|x| return false if not yield x}&lt;br /&gt;
true&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Array&lt;br /&gt;
include Quantifier&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts [1, 2, 3].any? {|x| x == 5}&lt;br /&gt;
puts [4, 3, 4].all? {|x| x == 4}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
As has been demonstrated in the previous examples and explanations, &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; allows programmers to pass instructions to objects in Ruby that can be called at any time as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson] - 2012&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow] - 2010&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow] - 2010&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint] - 2012&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Flectures%2Fnotes%2Fol5.pdf Class Notes CSC517 - Lecture 5] - 2010&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67515</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67515"/>
		<updated>2012-10-11T02:57:50Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref name=&amp;quot;tutorialspoint&amp;quot;&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm - 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref name=&amp;quot;tutorialspoint&amp;quot;/&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref name=&amp;quot;saas&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// List defined above&lt;br /&gt;
listIterator litr = l.listIterator(); &lt;br /&gt;
while(litr.hasNext()) {&lt;br /&gt;
    Object element = litr.next(); &lt;br /&gt;
    element.foo();&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# List defined above&lt;br /&gt;
list.each { |x| x.foo() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref name=&amp;quot;csc517exam&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf - 2008&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref name=&amp;quot;saas_pastebin&amp;quot;&amp;gt;http://pastebin.com/T3JhV7Bk - 2012&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref name=&amp;quot;saas&amp;quot;/&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
As has been demonstrated in the previous examples and explanations, &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; allows programmers to pass instructions to objects in Ruby that can be called at any time as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson] - 2012&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow] - 2010&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow] - 2010&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint] - 2012&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Flectures%2Fnotes%2Fol5.pdf Class Notes CSC517 - Lecture 5] - 2010&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67514</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67514"/>
		<updated>2012-10-11T02:55:41Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref name=&amp;quot;tutorialspoint&amp;quot;&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm - 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref name=&amp;quot;tutorialspoint&amp;quot;&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm - 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref name=&amp;quot;saas&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// List defined above&lt;br /&gt;
listIterator litr = l.listIterator(); &lt;br /&gt;
while(litr.hasNext()) {&lt;br /&gt;
    Object element = litr.next(); &lt;br /&gt;
    element.foo();&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# List defined above&lt;br /&gt;
list.each { |x| x.foo() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref name=&amp;quot;csc517exam&amp;quot;&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf - 2008&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref name=&amp;quot;saas_pastebin&amp;quot;&amp;gt;http://pastebin.com/T3JhV7Bk - 2012&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref name=&amp;quot;saas&amp;quot;&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
As has been demonstrated in the previous examples and explanations, &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; allows programmers to pass instructions to objects in Ruby that can be called at any time as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson] - 2012&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow] - 2010&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow] - 2010&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint] - 2012&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Flectures%2Fnotes%2Fol5.pdf Class Notes CSC517 - Lecture 5] - 2010&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67512</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67512"/>
		<updated>2012-10-11T02:43:36Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm - 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm - 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// List defined above&lt;br /&gt;
listIterator litr = l.listIterator(); &lt;br /&gt;
while(litr.hasNext()) {&lt;br /&gt;
    Object element = litr.next(); &lt;br /&gt;
    element.foo();&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# List defined above&lt;br /&gt;
list.each { |x| x.foo() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf - 2008&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk - 2012&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
As has been demonstrated in the previous examples and explanations, &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; allows programmers to pass instructions to objects in Ruby that can be called at any time as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson] - 2012&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow] - 2010&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow] - 2010&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint] - 2012&lt;br /&gt;
*[https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Flectures%2Fnotes%2Fol5.pdf Class Notes CSC517 - Lecture 5] - 2010&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67511</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67511"/>
		<updated>2012-10-11T02:41:17Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm - 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm - 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// List defined above&lt;br /&gt;
listIterator litr = l.listIterator(); &lt;br /&gt;
while(litr.hasNext()) {&lt;br /&gt;
    Object element = litr.next(); &lt;br /&gt;
    element.foo();&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# List defined above&lt;br /&gt;
list.each { |x| x.foo() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf - 2008&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk - 2012&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
As has been demonstrated in the previous examples and explanations, &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; allows programmers to pass instructions to objects in Ruby that can be called at any time as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson] - 2012&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow] - 2010&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow] - 2010&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint] - 2012&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67510</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67510"/>
		<updated>2012-10-11T02:40:35Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm - 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm - 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// List defined above&lt;br /&gt;
listIterator litr = l.listIterator(); &lt;br /&gt;
while(litr.hasNext()) {&lt;br /&gt;
    Object element = litr.next(); &lt;br /&gt;
    element.foo();&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# List defined above&lt;br /&gt;
list.each { |x| x.foo() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf - 2008&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk - 2012&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
As has been demonstrated in the previous examples and explanations, &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; allows programmers to pass instructions to objects in Ruby that can be called at any time as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson - 2012]&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow - 2010]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow - 2010]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint - 2012]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67509</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67509"/>
		<updated>2012-10-11T02:37:02Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm - 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm - 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// List defined above&lt;br /&gt;
listIterator litr = l.listIterator(); &lt;br /&gt;
while(litr.hasNext()) {&lt;br /&gt;
    Object element = litr.next(); &lt;br /&gt;
    element.foo();&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# List defined above&lt;br /&gt;
list.each { |x| x.foo() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf - 2008&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk - 2012&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw - 2012&amp;lt;/ref&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
As has been demonstrated in the previous examples and explanations, &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; allows programmers to pass instructions to objects in Ruby that can be called at any time as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson]&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67508</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67508"/>
		<updated>2012-10-11T02:35:55Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm 2012&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// List defined above&lt;br /&gt;
listIterator litr = l.listIterator(); &lt;br /&gt;
while(litr.hasNext()) {&lt;br /&gt;
    Object element = litr.next(); &lt;br /&gt;
    element.foo();&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# List defined above&lt;br /&gt;
list.each { |x| x.foo() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
As has been demonstrated in the previous examples and explanations, &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; allows programmers to pass instructions to objects in Ruby that can be called at any time as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson]&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67505</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67505"/>
		<updated>2012-10-11T02:21:00Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Using yield for Iterating */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// List defined above&lt;br /&gt;
listIterator litr = l.listIterator(); &lt;br /&gt;
while(litr.hasNext()) {&lt;br /&gt;
    Object element = litr.next(); &lt;br /&gt;
    element.foo();&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# List defined above&lt;br /&gt;
list.each { |x| x.foo() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
As has been demonstrated in the previous examples and explanations, &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; allows programmers to pass instructions to objects in Ruby that can be called at any time as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson]&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67504</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67504"/>
		<updated>2012-10-11T02:20:17Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Using yield for Iterating */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&lt;br /&gt;
// List defined above&lt;br /&gt;
listIterator litr = l.listIterator(); &lt;br /&gt;
while(litr.hasNext()) {&lt;br /&gt;
    Object element = litr.next(); &lt;br /&gt;
    element.foo();&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&lt;br /&gt;
# List defined above&lt;br /&gt;
list.each { |x| x.foo() }&lt;br /&gt;
&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
As has been demonstrated in the previous examples and explanations, &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; allows programmers to pass instructions to objects in Ruby that can be called at any time as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson]&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67503</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67503"/>
		<updated>2012-10-11T02:09:38Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
As has been demonstrated in the previous examples and explanations, &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; allows programmers to pass instructions to objects in Ruby that can be called at any time as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson]&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67502</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67502"/>
		<updated>2012-10-11T02:07:29Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* SaaS - 3.8 yield() */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
As has been demonstrated in the previous examples and explanations, &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; allows programmers to pass instructions to objects in Ruby as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson]&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67479</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67479"/>
		<updated>2012-10-11T01:26:54Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass [http://en.wikipedia.org/wiki/Code_block blocks] of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of [http://en.wikipedia.org/wiki/Iterator iterators] inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a [http://en.wikipedia.org/wiki/Namespace#Namespaces_in_Programming_Languages namespace] when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson]&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67477</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=67477"/>
		<updated>2012-10-11T01:22:10Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other object [http://en.wikipedia.org/wiki/Object-oriented_programming oriented languages].&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass blocks of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of iterators inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a namespace when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson]&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=67127</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=67127"/>
		<updated>2012-10-04T01:54:33Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: Added Alan and Chris's submission 1b 1w60&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 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;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66471</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66471"/>
		<updated>2012-10-03T20:03:33Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other object oriented languages.&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass blocks of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of iterators inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a namespace when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson]&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66470</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66470"/>
		<updated>2012-10-03T20:02:12Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* The yield() Function */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.example.com link title]=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other object oriented languages.&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass blocks of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of iterators inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a namespace when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
====Random Sequence====&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson]&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66469</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66469"/>
		<updated>2012-10-03T20:01:23Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Using yield for Iterating */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.example.com link title]=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other object oriented languages.&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass blocks of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video&amp;lt;ref&amp;gt;http://www.youtube.com/watch?v=yMV7nOiTwXw&amp;lt;/ref&amp;gt; focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of iterators inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a namespace when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
===Random Sequence===&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson]&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66468</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66468"/>
		<updated>2012-10-03T20:00:59Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.example.com link title]=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other object oriented languages.&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass blocks of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of iterators inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a namespace when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
===Random Sequence===&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.youtube.com/watch?v=yMV7nOiTwXw 3.8- yield()-SaaS-Armando Fox and David Patterson]&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66467</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66467"/>
		<updated>2012-10-03T20:00:07Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Comparison to Other Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.example.com link title]=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other object oriented languages.&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass blocks of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Using yield for Iterating===&lt;br /&gt;
The SaaS/Coursera video focuses on explaining the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function as it relates to iterating over collections and explains it like so:&lt;br /&gt;
&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of iterators inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a namespace when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
===Random Sequence===&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66285</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66285"/>
		<updated>2012-10-03T04:03:01Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Comparison to Other Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.example.com link title]=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other object oriented languages.&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass blocks of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Comparison to Other Languages===&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of iterators inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a namespace when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
===Random Sequence===&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66284</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66284"/>
		<updated>2012-10-03T04:02:24Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: /* Comparison to Other Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.example.com link title]=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other object oriented languages.&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass blocks of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Comparison to Other Languages===&lt;br /&gt;
The idea of using a &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of iterators inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:&lt;br /&gt;
'''Java''': Give me each element of a collection - I want to do something to each one.&lt;br /&gt;
'''Ruby''': Here is some code - do this to each element of this collection.&lt;br /&gt;
This idea of sending a function to an object, rather than sending object to function creates a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a namespace when calling a block of code on them.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
===Random Sequence===&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66264</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66264"/>
		<updated>2012-10-03T03:56:39Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.example.com link title]=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other object oriented languages.&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass blocks of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Why It's Useful===&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
===Random Sequence===&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby Blocks and Yields in Ruby @ StackOverflow]&lt;br /&gt;
*[http://stackoverflow.com/questions/4321737/what-does-the-yield-keyword-do-in-ruby What does the &amp;quot;yield&amp;quot; keyword do in Ruby? @ StackOverflow]&lt;br /&gt;
*[http://www.tutorialspoint.com/ruby/ruby_blocks.htm Ruby Blocks @ tutorialspoint]&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66253</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w60 ac</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w60_ac&amp;diff=66253"/>
		<updated>2012-10-03T03:51:45Z</updated>

		<summary type="html">&lt;p&gt;Cssuich: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SaaS - 3.8 yield()=&lt;br /&gt;
&lt;br /&gt;
==Introduction (Preface)==&lt;br /&gt;
In order to properly understand and cover the idea of how the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function works, it is necessary to understand a few concepts that do not exist in other object oriented languages.&lt;br /&gt;
===Code Blocks &amp;amp; Closures===&lt;br /&gt;
Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass blocks of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
codeblock = {puts &amp;quot;foo&amp;quot;}&lt;br /&gt;
codeblock.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code will print:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function does.&lt;br /&gt;
&lt;br /&gt;
An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.&lt;br /&gt;
===The Map &amp;amp; Grep Functions===&lt;br /&gt;
A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example subtracts 2 from every element of the list&lt;br /&gt;
mylist = mylist.map{|x| x-2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This code example returns all elements less than 5&lt;br /&gt;
arr = arr.grep(|x| x&amp;lt;5) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied. &lt;br /&gt;
==The yield() Function==&lt;br /&gt;
===What Is It?===&lt;br /&gt;
Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   puts &amp;quot;You are in the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
   puts &amp;quot;You are again back to the method&amp;quot;&lt;br /&gt;
   yield&lt;br /&gt;
end&lt;br /&gt;
test {puts &amp;quot;You are in the block&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the method&lt;br /&gt;
You are in the block&lt;br /&gt;
You are again back to the method&lt;br /&gt;
You are in the block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, take a look at the last line of code. You'll notice that the parameter to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; function is actually a code block. Next, look at where the text &amp;quot;You are in the block&amp;quot; appears: after the &amp;quot;You are in the method&amp;quot; text then again after the &amp;quot;You are again back to the method&amp;quot; text. Notice how this corresponds to where the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statements are in the code? That's because the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement is calling the code block, then returning control to the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method.&lt;br /&gt;
===Syntax===&lt;br /&gt;
As you may have guessed from the previous example, the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function's syntax is quite simple. In it's most basic form, the yield function can be called with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
yield parameter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Passing parameters allows you to call the same code block, but with different input. For example&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_blocks.htm&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def test&lt;br /&gt;
   yield 5&lt;br /&gt;
   puts &amp;quot;You are in the method test&amp;quot;&lt;br /&gt;
   yield 100&lt;br /&gt;
end&lt;br /&gt;
test {|i| puts &amp;quot;You are in the block #{i}&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Executing this code results in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You are in the block 5&lt;br /&gt;
You are in the method test&lt;br /&gt;
You are in the block 100&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
See how the same code block results in different output yielded to the first and second times? That's because the values &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; are passed each time, respectively, and output by the code block.&lt;br /&gt;
===Why It's Useful===&lt;br /&gt;
===Examples===&lt;br /&gt;
====Tree Traversal====&lt;br /&gt;
&amp;lt;ref&amp;gt;https://docs.google.com/viewer?url=http%3A%2F%2Fcourses.ncsu.edu%2Fcsc517%2Fcommon%2Fhomework%2Ftests%2Fa1.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Tree&lt;br /&gt;
  attr_reader :value&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @value = value&lt;br /&gt;
    @children = []&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def &amp;lt;&amp;lt;(value)&lt;br /&gt;
    subtree = Tree.new(value)&lt;br /&gt;
    @children &amp;lt;&amp;lt; subtree&lt;br /&gt;
    return subtree&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def each&lt;br /&gt;
    yield value&lt;br /&gt;
    @children.each do |child_node|&lt;br /&gt;
      child_node.each { |e| yield e }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
t = Tree.new()&lt;br /&gt;
... # code to populate the tree&lt;br /&gt;
t.each {|x| puts x}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child.&lt;br /&gt;
In this example, we create a class that includes Enumerable, providing access to the &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; method, among others.&lt;br /&gt;
===Random Sequence===&lt;br /&gt;
&amp;lt;ref&amp;gt;http://pastebin.com/T3JhV7Bk&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RandomSequence&lt;br /&gt;
  def initialize(limit,num)&lt;br /&gt;
    @limit,@num = limit,num&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @num.times { yield (rand * @limit).floor }&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
i = -1&lt;br /&gt;
RandomSequence.new(10,4).each do |num|&lt;br /&gt;
  i = num if i &amp;lt; num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This example comes from the SaaS video on the Ruby &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time &amp;lt;code&amp;gt;each&amp;lt;/code&amp;gt; yields to the code block, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; will be updated if the parameter to &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is the smallest value thus far. This is a great example of how passing a parameter to the &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; function allows for concise code.&lt;br /&gt;
==Further Readings==&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cssuich</name></author>
	</entry>
</feed>