<?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=Vsuresw</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=Vsuresw"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Vsuresw"/>
	<updated>2026-05-13T08:07:25Z</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_2011/ch4_4h_sv&amp;diff=54160</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54160"/>
		<updated>2011-10-25T05:25:26Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns][http://en.wikipedia.org/wiki/Singleton_pattern (Wiki)]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern][http://en.wikipedia.org/wiki/Adapter_pattern  (Wiki) ], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54159</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54159"/>
		<updated>2011-10-25T05:21:32Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Closures and Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns][http://en.wikipedia.org/wiki/Singleton_pattern (Wiki)]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern][http://en.wikipedia.org/wiki/Adapter_pattern  (Wiki) ], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54158</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54158"/>
		<updated>2011-10-25T05:21:14Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Closures and Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns][http://en.wikipedia.org/wiki/Singleton_pattern (Wiki)]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern][http://en.wikipedia.org/wiki/Adapter_pattern  (Wiki) ], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54157</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54157"/>
		<updated>2011-10-25T05:17:17Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Adapter Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns][http://en.wikipedia.org/wiki/Singleton_pattern (Wiki)]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern][http://en.wikipedia.org/wiki/Adapter_pattern  (Wiki) ], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54156</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54156"/>
		<updated>2011-10-25T05:15:21Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Singleton Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns][http://en.wikipedia.org/wiki/Singleton_pattern (Wiki)]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern][http://en.wikipedia.org/wiki/Adapter_pattern  (Wiki) ], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54155</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54155"/>
		<updated>2011-10-25T04:38:22Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Adapter Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns][http://en.wikipedia.org/wiki/Singleton_pattern (Wiki)]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern][http://en.wikipedia.org/wiki/Adapter_pattern  (Wiki) ], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54154</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54154"/>
		<updated>2011-10-25T04:36:46Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Singleton Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns][http://en.wikipedia.org/wiki/Singleton_pattern (Wiki)]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54153</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54153"/>
		<updated>2011-10-25T04:18:43Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Adapter Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53208</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53208"/>
		<updated>2011-10-20T20:45:07Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Command Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Observer Design Pattern''''', which defines a one - to - many dependency among objects such that, the change in state of an object causes all its dependents to be notified and updated automatically.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Observer Pattern==&lt;br /&gt;
&lt;br /&gt;
The Observer Pattern, also known as the Dependents or Publish - Subscribe Pattern, defines a one-to-many dependency between objects such that the change in state of an object causes the dependent objects to be notified and updated automatically. &lt;br /&gt;
&lt;br /&gt;
Partitioning of an application into a collection of co-operating classes, would require consistency to be maintained among these classes.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Command patterns lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an illustration of it:&lt;br /&gt;
&lt;br /&gt;
 class RoutePlanner&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def go(pointA, pointB)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def fastest … end&lt;br /&gt;
     def shortest … end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Here, we have a class which helps us plan the route between two points A and B, depending upon whether we want the shortest path or the fastest path.&lt;br /&gt;
&lt;br /&gt;
 ctx = RoutePlanner.new&lt;br /&gt;
 if on_foot&lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:shortest)&lt;br /&gt;
 else &lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:fastest)&lt;br /&gt;
 ctx.go(pointA, pointB)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the value of the variable on_foot which is not known until runtime. Therefore, the algorithm that will be made use of to reach B starting from A, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=52920</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=52920"/>
		<updated>2011-10-20T04:54:57Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Singleton Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Observer Design Pattern''''', which defines a one - to - many dependency among objects such that, the change in state of an object causes all its dependents to be notified and updated automatically.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Observer Pattern==&lt;br /&gt;
&lt;br /&gt;
The Observer Pattern, also known as the Dependents or Publish - Subscribe Pattern, defines a one-to-many dependency between objects such that the change in state of an object causes the dependent objects to be notified and updated automatically. &lt;br /&gt;
&lt;br /&gt;
Partitioning of an application into a collection of co-operating classes, would require consistency to be maintained among these classes.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
At times, it is needed to pass around code that needs to be executed later. This is achieved by employing the Command Design Pattern which is used to factor out the action code of a class into its own object. It encapsulates a request as an object, allowing clients with different requests to be parameterized. The Command Pattern is implemented using Closures. A Command class holds an object, method along with the information needed to call a method at a later time. This information includes the method name with the values for the method parameters. The 'Call' method of Ruby brings the different parts together when needed.&lt;br /&gt;
&lt;br /&gt;
This is illustrated using the following example:&lt;br /&gt;
&lt;br /&gt;
One example is a check at a restaurant.&lt;br /&gt;
* The waiter/waitress takes an order from a customer and writes it on a check.&lt;br /&gt;
* The check is then queued for the cook, who prepares the food as requested by the customer.&lt;br /&gt;
* The check is later returned to the server, who uses it to bill the customer.&lt;br /&gt;
&lt;br /&gt;
In this example, the check has nothing to do with the menu. In principle, the same checks could be used at any restaurant.&lt;br /&gt;
&lt;br /&gt;
The command pattern is made up of a client, invoker and receiver. A client creates an object of the Command class and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is the class object which contains the code of the method. The usage of command objects makes it easier to construct components which need to execute methods at a later time without having knowledge about the method's owner or parameters.&lt;br /&gt;
&lt;br /&gt;
This Pattern can be implemented using the Proc objects, which is a callable block of code that closes over the variables in scope when it was created. This gives us a concise implementation of Command Pattern.&lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the Command Design Pattern, using Proc Objects:&lt;br /&gt;
&lt;br /&gt;
 count = 0&lt;br /&gt;
 commands = []&lt;br /&gt;
 (1..10).each do |i|&lt;br /&gt;
 commands &amp;lt;&amp;lt; proc { count += i }&lt;br /&gt;
 end&lt;br /&gt;
 puts &amp;quot;Count is initially #{count}&amp;quot;&lt;br /&gt;
 commands.each { |cmd| cmd.call }&lt;br /&gt;
 puts &amp;quot;Performed all commands. count is #{count}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an illustration of it:&lt;br /&gt;
&lt;br /&gt;
 class RoutePlanner&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def go(pointA, pointB)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def fastest … end&lt;br /&gt;
     def shortest … end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Here, we have a class which helps us plan the route between two points A and B, depending upon whether we want the shortest path or the fastest path.&lt;br /&gt;
&lt;br /&gt;
 ctx = RoutePlanner.new&lt;br /&gt;
 if on_foot&lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:shortest)&lt;br /&gt;
 else &lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:fastest)&lt;br /&gt;
 ctx.go(pointA, pointB)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the value of the variable on_foot which is not known until runtime. Therefore, the algorithm that will be made use of to reach B starting from A, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=52918</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=52918"/>
		<updated>2011-10-20T04:50:42Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Adapter Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Observer Design Pattern''''', which defines a one - to - many dependency among objects such that, the change in state of an object causes all its dependents to be notified and updated automatically.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
        That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Observer Pattern==&lt;br /&gt;
&lt;br /&gt;
The Observer Pattern, also known as the Dependents or Publish - Subscribe Pattern, defines a one-to-many dependency between objects such that the change in state of an object causes the dependent objects to be notified and updated automatically. &lt;br /&gt;
&lt;br /&gt;
Partitioning of an application into a collection of co-operating classes, would require consistency to be maintained among these classes.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
At times, it is needed to pass around code that needs to be executed later. This is achieved by employing the Command Design Pattern which is used to factor out the action code of a class into its own object. It encapsulates a request as an object, allowing clients with different requests to be parameterized. The Command Pattern is implemented using Closures. A Command class holds an object, method along with the information needed to call a method at a later time. This information includes the method name with the values for the method parameters. The 'Call' method of Ruby brings the different parts together when needed.&lt;br /&gt;
&lt;br /&gt;
This is illustrated using the following example:&lt;br /&gt;
&lt;br /&gt;
One example is a check at a restaurant.&lt;br /&gt;
* The waiter/waitress takes an order from a customer and writes it on a check.&lt;br /&gt;
* The check is then queued for the cook, who prepares the food as requested by the customer.&lt;br /&gt;
* The check is later returned to the server, who uses it to bill the customer.&lt;br /&gt;
&lt;br /&gt;
In this example, the check has nothing to do with the menu. In principle, the same checks could be used at any restaurant.&lt;br /&gt;
&lt;br /&gt;
The command pattern is made up of a client, invoker and receiver. A client creates an object of the Command class and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is the class object which contains the code of the method. The usage of command objects makes it easier to construct components which need to execute methods at a later time without having knowledge about the method's owner or parameters.&lt;br /&gt;
&lt;br /&gt;
This Pattern can be implemented using the Proc objects, which is a callable block of code that closes over the variables in scope when it was created. This gives us a concise implementation of Command Pattern.&lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the Command Design Pattern, using Proc Objects:&lt;br /&gt;
&lt;br /&gt;
 count = 0&lt;br /&gt;
 commands = []&lt;br /&gt;
 (1..10).each do |i|&lt;br /&gt;
 commands &amp;lt;&amp;lt; proc { count += i }&lt;br /&gt;
 end&lt;br /&gt;
 puts &amp;quot;Count is initially #{count}&amp;quot;&lt;br /&gt;
 commands.each { |cmd| cmd.call }&lt;br /&gt;
 puts &amp;quot;Performed all commands. count is #{count}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an illustration of it:&lt;br /&gt;
&lt;br /&gt;
 class RoutePlanner&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def go(pointA, pointB)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def fastest … end&lt;br /&gt;
     def shortest … end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Here, we have a class which helps us plan the route between two points A and B, depending upon whether we want the shortest path or the fastest path.&lt;br /&gt;
&lt;br /&gt;
 ctx = RoutePlanner.new&lt;br /&gt;
 if on_foot&lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:shortest)&lt;br /&gt;
 else &lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:fastest)&lt;br /&gt;
 ctx.go(pointA, pointB)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the value of the variable on_foot which is not known until runtime. Therefore, the algorithm that will be made use of to reach B starting from A, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=52574</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=52574"/>
		<updated>2011-10-19T04:43:06Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Singleton Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Observer Design Pattern''''', which defines a one - to - many dependency among objects such that, the change in state of an object causes all its dependents to be notified and updated automatically.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
        That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&lt;br /&gt;
* “Wrapping” its own interface around the interface of a pre-existing class. &lt;br /&gt;
* Besides, it may also translate data formats from the caller to a form needed by the callee. Example: If the caller stores boolean value in terms of integers but the callee needs the values as 'true/false', the adapter pattern would extract the right value from the caller and pass it on to the callee. This ensures that the caller and callee can work together. &lt;br /&gt;
&lt;br /&gt;
The purpose of an adapter is “to convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.”&lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the Adapter Pattern:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Adapter Patterns===&lt;br /&gt;
&lt;br /&gt;
Based on the structure, there are two types of Adapter Patterns:&lt;br /&gt;
&lt;br /&gt;
====Object Adapter Pattern====&lt;br /&gt;
&lt;br /&gt;
The Adapter Pattern has an instance of the classit &amp;quot;wraps&amp;quot; and makes calls to this wrapped object alone.&lt;br /&gt;
&lt;br /&gt;
====Class Adapter Pattern====&lt;br /&gt;
&lt;br /&gt;
Polymorphic Interfaces are used by this type of pattern, where the interface that is expected and the interface that is available are both inherited or implemented. This is used in situations where the class providing the services you need does not provide the interface required.&lt;br /&gt;
&lt;br /&gt;
==Observer Pattern==&lt;br /&gt;
&lt;br /&gt;
The Observer Pattern, also known as the Dependents or Publish - Subscribe Pattern, defines a one-to-many dependency between objects such that the change in state of an object causes the dependent objects to be notified and updated automatically. &lt;br /&gt;
&lt;br /&gt;
Partitioning of an application into a collection of co-operating classes, would require consistency to be maintained among these classes.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
At times, it is needed to pass around code that needs to be executed later. This is achieved by employing the Command Design Pattern which is used to factor out the action code of a class into its own object. It encapsulates a request as an object, allowing clients with different requests to be parameterized. The Command Pattern is implemented using Closures. A Command class holds an object, method along with the information needed to call a method at a later time. This information includes the method name with the values for the method parameters. The 'Call' method of Ruby brings the different parts together when needed.&lt;br /&gt;
&lt;br /&gt;
This is illustrated using the following example:&lt;br /&gt;
&lt;br /&gt;
One example is a check at a restaurant.&lt;br /&gt;
* The waiter/waitress takes an order from a customer and writes it on a check.&lt;br /&gt;
* The check is then queued for the cook, who prepares the food as requested by the customer.&lt;br /&gt;
* The check is later returned to the server, who uses it to bill the customer.&lt;br /&gt;
&lt;br /&gt;
In this example, the check has nothing to do with the menu. In principle, the same checks could be used at any restaurant.&lt;br /&gt;
&lt;br /&gt;
The command pattern is made up of a client, invoker and receiver. A client creates an object of the Command class and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is the class object which contains the code of the method. The usage of command objects makes it easier to construct components which need to execute methods at a later time without having knowledge about the method's owner or parameters.&lt;br /&gt;
&lt;br /&gt;
This Pattern can be implemented using the Proc objects, which is a callable block of code that closes over the variables in scope when it was created. This gives us a concise implementation of Command Pattern.&lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the Command Design Pattern, using Proc Objects:&lt;br /&gt;
&lt;br /&gt;
 count = 0&lt;br /&gt;
 commands = []&lt;br /&gt;
 (1..10).each do |i|&lt;br /&gt;
 commands &amp;lt;&amp;lt; proc { count += i }&lt;br /&gt;
 end&lt;br /&gt;
 puts &amp;quot;Count is initially #{count}&amp;quot;&lt;br /&gt;
 commands.each { |cmd| cmd.call }&lt;br /&gt;
 puts &amp;quot;Performed all commands. count is #{count}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an illustration of it:&lt;br /&gt;
&lt;br /&gt;
 class RoutePlanner&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def go(pointA, pointB)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def fastest … end&lt;br /&gt;
     def shortest … end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Here, we have a class which helps us plan the route between two points A and B, depending upon whether we want the shortest path or the fastest path.&lt;br /&gt;
&lt;br /&gt;
 ctx = RoutePlanner.new&lt;br /&gt;
 if on_foot&lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:shortest)&lt;br /&gt;
 else &lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:fastest)&lt;br /&gt;
 ctx.go(pointA, pointB)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the value of the variable on_foot which is not known until runtime. Therefore, the algorithm that will be made use of to reach B starting from A, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=52573</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=52573"/>
		<updated>2011-10-19T04:41:40Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Singleton Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Observer Design Pattern''''', which defines a one - to - many dependency among objects such that, the change in state of an object causes all its dependents to be notified and updated automatically.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
        That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &amp;gt;Initialised&lt;br /&gt;
 &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
 The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
 The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&lt;br /&gt;
* “Wrapping” its own interface around the interface of a pre-existing class. &lt;br /&gt;
* Besides, it may also translate data formats from the caller to a form needed by the callee. Example: If the caller stores boolean value in terms of integers but the callee needs the values as 'true/false', the adapter pattern would extract the right value from the caller and pass it on to the callee. This ensures that the caller and callee can work together. &lt;br /&gt;
&lt;br /&gt;
The purpose of an adapter is “to convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.”&lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the Adapter Pattern:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Adapter Patterns===&lt;br /&gt;
&lt;br /&gt;
Based on the structure, there are two types of Adapter Patterns:&lt;br /&gt;
&lt;br /&gt;
====Object Adapter Pattern====&lt;br /&gt;
&lt;br /&gt;
The Adapter Pattern has an instance of the classit &amp;quot;wraps&amp;quot; and makes calls to this wrapped object alone.&lt;br /&gt;
&lt;br /&gt;
====Class Adapter Pattern====&lt;br /&gt;
&lt;br /&gt;
Polymorphic Interfaces are used by this type of pattern, where the interface that is expected and the interface that is available are both inherited or implemented. This is used in situations where the class providing the services you need does not provide the interface required.&lt;br /&gt;
&lt;br /&gt;
==Observer Pattern==&lt;br /&gt;
&lt;br /&gt;
The Observer Pattern, also known as the Dependents or Publish - Subscribe Pattern, defines a one-to-many dependency between objects such that the change in state of an object causes the dependent objects to be notified and updated automatically. &lt;br /&gt;
&lt;br /&gt;
Partitioning of an application into a collection of co-operating classes, would require consistency to be maintained among these classes.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
At times, it is needed to pass around code that needs to be executed later. This is achieved by employing the Command Design Pattern which is used to factor out the action code of a class into its own object. It encapsulates a request as an object, allowing clients with different requests to be parameterized. The Command Pattern is implemented using Closures. A Command class holds an object, method along with the information needed to call a method at a later time. This information includes the method name with the values for the method parameters. The 'Call' method of Ruby brings the different parts together when needed.&lt;br /&gt;
&lt;br /&gt;
This is illustrated using the following example:&lt;br /&gt;
&lt;br /&gt;
One example is a check at a restaurant.&lt;br /&gt;
* The waiter/waitress takes an order from a customer and writes it on a check.&lt;br /&gt;
* The check is then queued for the cook, who prepares the food as requested by the customer.&lt;br /&gt;
* The check is later returned to the server, who uses it to bill the customer.&lt;br /&gt;
&lt;br /&gt;
In this example, the check has nothing to do with the menu. In principle, the same checks could be used at any restaurant.&lt;br /&gt;
&lt;br /&gt;
The command pattern is made up of a client, invoker and receiver. A client creates an object of the Command class and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is the class object which contains the code of the method. The usage of command objects makes it easier to construct components which need to execute methods at a later time without having knowledge about the method's owner or parameters.&lt;br /&gt;
&lt;br /&gt;
This Pattern can be implemented using the Proc objects, which is a callable block of code that closes over the variables in scope when it was created. This gives us a concise implementation of Command Pattern.&lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the Command Design Pattern, using Proc Objects:&lt;br /&gt;
&lt;br /&gt;
 count = 0&lt;br /&gt;
 commands = []&lt;br /&gt;
 (1..10).each do |i|&lt;br /&gt;
 commands &amp;lt;&amp;lt; proc { count += i }&lt;br /&gt;
 end&lt;br /&gt;
 puts &amp;quot;Count is initially #{count}&amp;quot;&lt;br /&gt;
 commands.each { |cmd| cmd.call }&lt;br /&gt;
 puts &amp;quot;Performed all commands. count is #{count}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an illustration of it:&lt;br /&gt;
&lt;br /&gt;
 class RoutePlanner&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def go(pointA, pointB)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def fastest … end&lt;br /&gt;
     def shortest … end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Here, we have a class which helps us plan the route between two points A and B, depending upon whether we want the shortest path or the fastest path.&lt;br /&gt;
&lt;br /&gt;
 ctx = RoutePlanner.new&lt;br /&gt;
 if on_foot&lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:shortest)&lt;br /&gt;
 else &lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:fastest)&lt;br /&gt;
 ctx.go(pointA, pointB)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the value of the variable on_foot which is not known until runtime. Therefore, the algorithm that will be made use of to reach B starting from A, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=52572</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=52572"/>
		<updated>2011-10-19T04:40:46Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Observer Design Pattern''''', which defines a one - to - many dependency among objects such that, the change in state of an object causes all its dependents to be notified and updated automatically.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
        That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &amp;gt;Initialised&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
 The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
 The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&lt;br /&gt;
* “Wrapping” its own interface around the interface of a pre-existing class. &lt;br /&gt;
* Besides, it may also translate data formats from the caller to a form needed by the callee. Example: If the caller stores boolean value in terms of integers but the callee needs the values as 'true/false', the adapter pattern would extract the right value from the caller and pass it on to the callee. This ensures that the caller and callee can work together. &lt;br /&gt;
&lt;br /&gt;
The purpose of an adapter is “to convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.”&lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the Adapter Pattern:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Adapter Patterns===&lt;br /&gt;
&lt;br /&gt;
Based on the structure, there are two types of Adapter Patterns:&lt;br /&gt;
&lt;br /&gt;
====Object Adapter Pattern====&lt;br /&gt;
&lt;br /&gt;
The Adapter Pattern has an instance of the classit &amp;quot;wraps&amp;quot; and makes calls to this wrapped object alone.&lt;br /&gt;
&lt;br /&gt;
====Class Adapter Pattern====&lt;br /&gt;
&lt;br /&gt;
Polymorphic Interfaces are used by this type of pattern, where the interface that is expected and the interface that is available are both inherited or implemented. This is used in situations where the class providing the services you need does not provide the interface required.&lt;br /&gt;
&lt;br /&gt;
==Observer Pattern==&lt;br /&gt;
&lt;br /&gt;
The Observer Pattern, also known as the Dependents or Publish - Subscribe Pattern, defines a one-to-many dependency between objects such that the change in state of an object causes the dependent objects to be notified and updated automatically. &lt;br /&gt;
&lt;br /&gt;
Partitioning of an application into a collection of co-operating classes, would require consistency to be maintained among these classes.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
At times, it is needed to pass around code that needs to be executed later. This is achieved by employing the Command Design Pattern which is used to factor out the action code of a class into its own object. It encapsulates a request as an object, allowing clients with different requests to be parameterized. The Command Pattern is implemented using Closures. A Command class holds an object, method along with the information needed to call a method at a later time. This information includes the method name with the values for the method parameters. The 'Call' method of Ruby brings the different parts together when needed.&lt;br /&gt;
&lt;br /&gt;
This is illustrated using the following example:&lt;br /&gt;
&lt;br /&gt;
One example is a check at a restaurant.&lt;br /&gt;
* The waiter/waitress takes an order from a customer and writes it on a check.&lt;br /&gt;
* The check is then queued for the cook, who prepares the food as requested by the customer.&lt;br /&gt;
* The check is later returned to the server, who uses it to bill the customer.&lt;br /&gt;
&lt;br /&gt;
In this example, the check has nothing to do with the menu. In principle, the same checks could be used at any restaurant.&lt;br /&gt;
&lt;br /&gt;
The command pattern is made up of a client, invoker and receiver. A client creates an object of the Command class and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is the class object which contains the code of the method. The usage of command objects makes it easier to construct components which need to execute methods at a later time without having knowledge about the method's owner or parameters.&lt;br /&gt;
&lt;br /&gt;
This Pattern can be implemented using the Proc objects, which is a callable block of code that closes over the variables in scope when it was created. This gives us a concise implementation of Command Pattern.&lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the Command Design Pattern, using Proc Objects:&lt;br /&gt;
&lt;br /&gt;
 count = 0&lt;br /&gt;
 commands = []&lt;br /&gt;
 (1..10).each do |i|&lt;br /&gt;
 commands &amp;lt;&amp;lt; proc { count += i }&lt;br /&gt;
 end&lt;br /&gt;
 puts &amp;quot;Count is initially #{count}&amp;quot;&lt;br /&gt;
 commands.each { |cmd| cmd.call }&lt;br /&gt;
 puts &amp;quot;Performed all commands. count is #{count}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an illustration of it:&lt;br /&gt;
&lt;br /&gt;
 class RoutePlanner&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def go(pointA, pointB)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def fastest … end&lt;br /&gt;
     def shortest … end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Here, we have a class which helps us plan the route between two points A and B, depending upon whether we want the shortest path or the fastest path.&lt;br /&gt;
&lt;br /&gt;
 ctx = RoutePlanner.new&lt;br /&gt;
 if on_foot&lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:shortest)&lt;br /&gt;
 else &lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:fastest)&lt;br /&gt;
 ctx.go(pointA, pointB)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the value of the variable on_foot which is not known until runtime. Therefore, the algorithm that will be made use of to reach B starting from A, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51961</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51961"/>
		<updated>2011-10-15T00:42:24Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Currying */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamic], [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 reflective], [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented], [http://en.wikipedia.org/wiki/Interpreted_language single-pass interpreted] programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports [http://en.wikipedia.org/wiki/Functional_programming functional], [http://en.wikipedia.org/wiki/Imperative_programming imperative], reflective, object oriented and many other programming paradigms. Since Ruby was designed to incorporate the best features of scripting and Object oriented languages, Ruby emerged to be a very powerful language with native support for many features that are not in the 'nature' of other languages like Java and C++. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro &amp;quot;Matz&amp;quot; Matsumoto]. The intent of developing Ruby was to have a new language that balances functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than [http://www.perl.org/ Perl] and more Object Oriented than [http://www.python.org/ Python].&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of [http://en.wikipedia.org/wiki/Ruby_on_rails Ruby on Rails] in 2005, an [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, [http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html TIOBE programming language popularity index] state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. [http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Purely object oriented]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. [http://c2.com/cgi/wiki?DynamicTyping Dynamically typed]'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Blocks and Closures]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. [http://www.rubyist.net/~slagell/ruby/iterators.html Built in Iterators]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Multiple Inheritance through Mixins]'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. [http://en.wikipedia.org/wiki/Duck_typing Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;]'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. [http://www.regular-expressions.info/ruby.html Syntax level Perl compatible for regular expressions]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. [http://designpatternsinruby.com/ Built-in support for certain Design Patterns]'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. [http://corelib.rubyonrails.org/classes/GC.html Mark-and-Sweep garbage collection]'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. [http://www.ruby-doc.org/core-1.9.2/Thread.html OS independent threading]'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. [http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Easy interface to C modules]'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. [http://www.ruby-lang.org/en/about/ Portable easily]'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
Here is a little table that highlights the key differences between java and ruby. Many of these exact difference maybe shared by Ruby and other languages like C,C++ etc.&amp;lt;br&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing. &lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Methods are passed to the objects&lt;br /&gt;
| Objects are passed to the methods&lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the [http://en.wikipedia.org/wiki/Interactive_Ruby_Shell IRB]. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by [http://www.eclipse.org/ Eclipse]. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
[http://www.jetbrains.com/ruby/ RubyMine] is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
The ''.rb'' extension is used to save Ruby files. The Ruby programs can be run by the command ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file. The path to the ruby command is specified here.&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
===Functions===&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. Ruby does not use { } parenthesis to specify scope. Instead, the keyword  '''end''' is equivalent to the closing parenthesis.&lt;br /&gt;
&lt;br /&gt;
  def fivetimes(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  fivetimes&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized in the function definition itself. The arguments can be specified without ( ) parentheses. This means parentheses in a function call are optional. We can write  ''fivetimes ( 4 )''  or  ''fivetimes 4'' . &lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greeting(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greeting('Sam', 'Peter', 'Derek')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Sam, Peter, Derek&lt;br /&gt;
&lt;br /&gt;
The user need not specify any return value or have an explicit ''return'' for a function.&lt;br /&gt;
The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object.&amp;lt;br&amp;gt;[http://rubylearning.com/satishtalim/writing_own_ruby_methods.html This] is really helpful to learn more about methods in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
One of the reasons that makes Ruby easier to program is the fact that the programmer need not specify the type for any variable. Variables in Ruby are not given a type. Their “type” depends upon the value assigned to them. This feature of Ruby is called [http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing].&lt;br /&gt;
&lt;br /&gt;
In the examples below, the variables ''PI'', ''a'', ''b'', ''c'' are not given a type by the programmer. At run time, ''PI'' is assigned type ''float'', ''a'' and ''b'' are assigned type ''int'', and ''c'' is assigned type ''array''. &lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  ''#''  and run to the end of the line. &lt;br /&gt;
&lt;br /&gt;
Dynamic typing makes many features like unbounded polymorphism, blocks and closures possible. These features are not available in statically typed languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers, strings and arrays are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;changetoupcase&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;CHANGETOUPCASE&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
  &lt;br /&gt;
  [1,3,6,8].class&lt;br /&gt;
  &amp;gt;&amp;gt;Array&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.sitepoint.com/typing-versus-dynamic-typing/ Here] is something we got off the internet which explains Dynamic typing well although its not particular to Ruby.&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An [http://www.ruby-doc.org/core-1.9.2/Array.html array] in Ruby is an ordered list of elements. An array can be considered an object of objects. In C, C++, Java, C# and other languages, an array consists of elements of a similar type. In Ruby, there is no such constraint. An array can consist of any object. We can have a single array that has Fixnum, String, Array etc as elements. &lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
  d = ['I am string', 4.5, 1000000, [1,2,3,4]] # elements of different type&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array is illustrated below:&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
  &lt;br /&gt;
  b &amp;lt;&amp;lt; [10,100]&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4, [10, 100]]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  ''b[‐1]''  refers to the last element of  ''b''. &lt;br /&gt;
For example, consider the array ''b'' above.&lt;br /&gt;
&lt;br /&gt;
  puts b[-1]&lt;br /&gt;
  &amp;gt;&amp;gt;[10, 100]&lt;br /&gt;
  &lt;br /&gt;
  puts b[-2]&lt;br /&gt;
  &amp;gt;&amp;gt;4&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][0]&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][1]&lt;br /&gt;
  &amp;gt;&amp;gt;100 &lt;br /&gt;
&lt;br /&gt;
Arrays can also be created using the  ''new''  keyword. ''new''  is used to create objects of any type. &lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Strings==&lt;br /&gt;
&lt;br /&gt;
A [http://www.rubyist.net/~slagell/ruby/strings.html String] in Ruby may be double-quoted (&amp;quot;...&amp;quot;) or single-quoted ('...'). &lt;br /&gt;
&lt;br /&gt;
However, double-quoting and single-quoting have different effects in some cases. A double-quoted string allows character escapes by a leading backslash, and the evaluation of embedded expressions using #{}. A single-quoted string does not interpret this. This is shown in the example below:&lt;br /&gt;
&lt;br /&gt;
  string1 = &amp;quot;abc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;abc&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  string2 = 'efg'&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;a\nb\nc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;a&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
  &amp;gt;&amp;gt;c&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts 'a\nb\n'&lt;br /&gt;
  &amp;gt;&amp;gt;a\nb\nc&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;abcd #{5*3} efg&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;abcd 15 efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts var = &amp;quot; abc &amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot; abc &amp;quot;&lt;br /&gt;
  puts &amp;quot;1234#{var}5678&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;1234 abc 5678&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby strings can be concatenated using + operator. A string can be repeated multiple times using * operator. The example below shows the use of + and * operators. Ruby string handling is more powerful than C. Since Ruby, provides support for regular expressions, string handling and pattern matching can be made very efficiently and easily. &lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; + &amp;quot;bar&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foobar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; * 2&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foofoo&amp;quot;&lt;br /&gt;
&lt;br /&gt;
===Symbols===&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols] in Ruby are Strings, just with an important difference, Symbols are immutable while Strings are not. Mutable objects can be changed after assignment while immutable objects can only be overwritten. Ruby is quite unique in offering mutable Strings, which adds greatly to its expressiveness. However mutable Strings can have their share of issues in terms of creating unexpected results and reduced performance. It is for this reason Ruby also offers programmers the choice of Symbols.&lt;br /&gt;
&lt;br /&gt;
Symbols need not contain only alphanumeric characters, by using quotes you can not only use the same characters as you would in a String, but also interpolate Symbols as well. Syntactically, a Symbol needs a colon : prepended. Symbols are actually so similar to Strings that converting between the two is very simple and consistent.&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello&amp;quot;&lt;br /&gt;
  :hello&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  bang = &amp;quot;!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; &amp;quot;hello world!&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; :&amp;quot;hello world!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ This] and [http://www.troubleshooters.com/codecorn/ruby/symbols.htm This] delve deeply into the difference between string and symbol. Well they are interesting worth reading atleast once.&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash] is a collection of Key-Value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.&lt;br /&gt;
&lt;br /&gt;
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. There are three different ways to create a Hashes. &lt;br /&gt;
&lt;br /&gt;
1. Using the keyword  '''new'''.&lt;br /&gt;
  &lt;br /&gt;
  z = Hash.new&lt;br /&gt;
  z[&amp;quot;a&amp;quot;] = 'Hello'&lt;br /&gt;
  z[&amp;quot;b&amp;quot;] = 'Hi'&lt;br /&gt;
  z[&amp;quot;c&amp;quot;] = 'Hey'&lt;br /&gt;
  &lt;br /&gt;
  puts z['a']&lt;br /&gt;
  &amp;gt;&amp;gt;Hello&lt;br /&gt;
  &lt;br /&gt;
  puts z['b']&lt;br /&gt;
  &amp;gt;&amp;gt;Hi&lt;br /&gt;
  &lt;br /&gt;
  puts z['c']&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
2. Using special syntax  '''Hash[ ]'''. &lt;br /&gt;
  &lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Block] is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
This example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you don't have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambdas in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterator] is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closure] in Ruby is a block that closes over. Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x). More about currying may be learnt from [http://en.wikipedia.org/wiki/Currying here]. Currying is a very interesting feature and together with closures, it provides a flexibility to ruby at ease which is not easily achievable in java or c++.&lt;br /&gt;
&amp;lt;br&amp;gt; [http://asemanfar.com/Currying-in-Ruby Here] is a set of nice programming examples in Ruby to explain/implement currying.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language that provides easy programming. The features of Ruby have made it a very popular and extensible language.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/ &amp;quot;Programming Ruby - The Pragmatic Programmer's Guide&amp;quot;]&amp;lt;br&amp;gt; &lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Object Oriented Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-lang.org/en/about/ Features of Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closures]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterators in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules and Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Duck_typing Duck Typing - Unbounded Polymorphism]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html Regular expressions]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interactive_Ruby_Shell Interactive Ruby Browser]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Array.html Array class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/strings.html String]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Blocks, Procs and Lambdas]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.csc.ncsu.edu/faculty/efg/517/f11/schedule CSC517 Class notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51960</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51960"/>
		<updated>2011-10-15T00:37:58Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Symbols */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamic], [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 reflective], [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented], [http://en.wikipedia.org/wiki/Interpreted_language single-pass interpreted] programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports [http://en.wikipedia.org/wiki/Functional_programming functional], [http://en.wikipedia.org/wiki/Imperative_programming imperative], reflective, object oriented and many other programming paradigms. Since Ruby was designed to incorporate the best features of scripting and Object oriented languages, Ruby emerged to be a very powerful language with native support for many features that are not in the 'nature' of other languages like Java and C++. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro &amp;quot;Matz&amp;quot; Matsumoto]. The intent of developing Ruby was to have a new language that balances functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than [http://www.perl.org/ Perl] and more Object Oriented than [http://www.python.org/ Python].&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of [http://en.wikipedia.org/wiki/Ruby_on_rails Ruby on Rails] in 2005, an [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, [http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html TIOBE programming language popularity index] state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. [http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Purely object oriented]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. [http://c2.com/cgi/wiki?DynamicTyping Dynamically typed]'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Blocks and Closures]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. [http://www.rubyist.net/~slagell/ruby/iterators.html Built in Iterators]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Multiple Inheritance through Mixins]'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. [http://en.wikipedia.org/wiki/Duck_typing Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;]'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. [http://www.regular-expressions.info/ruby.html Syntax level Perl compatible for regular expressions]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. [http://designpatternsinruby.com/ Built-in support for certain Design Patterns]'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. [http://corelib.rubyonrails.org/classes/GC.html Mark-and-Sweep garbage collection]'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. [http://www.ruby-doc.org/core-1.9.2/Thread.html OS independent threading]'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. [http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Easy interface to C modules]'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. [http://www.ruby-lang.org/en/about/ Portable easily]'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
Here is a little table that highlights the key differences between java and ruby. Many of these exact difference maybe shared by Ruby and other languages like C,C++ etc.&amp;lt;br&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing. &lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Methods are passed to the objects&lt;br /&gt;
| Objects are passed to the methods&lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the [http://en.wikipedia.org/wiki/Interactive_Ruby_Shell IRB]. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by [http://www.eclipse.org/ Eclipse]. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
[http://www.jetbrains.com/ruby/ RubyMine] is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
The ''.rb'' extension is used to save Ruby files. The Ruby programs can be run by the command ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file. The path to the ruby command is specified here.&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
===Functions===&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. Ruby does not use { } parenthesis to specify scope. Instead, the keyword  '''end''' is equivalent to the closing parenthesis.&lt;br /&gt;
&lt;br /&gt;
  def fivetimes(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  fivetimes&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized in the function definition itself. The arguments can be specified without ( ) parentheses. This means parentheses in a function call are optional. We can write  ''fivetimes ( 4 )''  or  ''fivetimes 4'' . &lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greeting(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greeting('Sam', 'Peter', 'Derek')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Sam, Peter, Derek&lt;br /&gt;
&lt;br /&gt;
The user need not specify any return value or have an explicit ''return'' for a function.&lt;br /&gt;
The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object.&amp;lt;br&amp;gt;[http://rubylearning.com/satishtalim/writing_own_ruby_methods.html This] is really helpful to learn more about methods in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
One of the reasons that makes Ruby easier to program is the fact that the programmer need not specify the type for any variable. Variables in Ruby are not given a type. Their “type” depends upon the value assigned to them. This feature of Ruby is called [http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing].&lt;br /&gt;
&lt;br /&gt;
In the examples below, the variables ''PI'', ''a'', ''b'', ''c'' are not given a type by the programmer. At run time, ''PI'' is assigned type ''float'', ''a'' and ''b'' are assigned type ''int'', and ''c'' is assigned type ''array''. &lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  ''#''  and run to the end of the line. &lt;br /&gt;
&lt;br /&gt;
Dynamic typing makes many features like unbounded polymorphism, blocks and closures possible. These features are not available in statically typed languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers, strings and arrays are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;changetoupcase&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;CHANGETOUPCASE&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
  &lt;br /&gt;
  [1,3,6,8].class&lt;br /&gt;
  &amp;gt;&amp;gt;Array&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.sitepoint.com/typing-versus-dynamic-typing/ Here] is something we got off the internet which explains Dynamic typing well although its not particular to Ruby.&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An [http://www.ruby-doc.org/core-1.9.2/Array.html array] in Ruby is an ordered list of elements. An array can be considered an object of objects. In C, C++, Java, C# and other languages, an array consists of elements of a similar type. In Ruby, there is no such constraint. An array can consist of any object. We can have a single array that has Fixnum, String, Array etc as elements. &lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
  d = ['I am string', 4.5, 1000000, [1,2,3,4]] # elements of different type&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array is illustrated below:&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
  &lt;br /&gt;
  b &amp;lt;&amp;lt; [10,100]&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4, [10, 100]]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  ''b[‐1]''  refers to the last element of  ''b''. &lt;br /&gt;
For example, consider the array ''b'' above.&lt;br /&gt;
&lt;br /&gt;
  puts b[-1]&lt;br /&gt;
  &amp;gt;&amp;gt;[10, 100]&lt;br /&gt;
  &lt;br /&gt;
  puts b[-2]&lt;br /&gt;
  &amp;gt;&amp;gt;4&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][0]&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][1]&lt;br /&gt;
  &amp;gt;&amp;gt;100 &lt;br /&gt;
&lt;br /&gt;
Arrays can also be created using the  ''new''  keyword. ''new''  is used to create objects of any type. &lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Strings==&lt;br /&gt;
&lt;br /&gt;
A [http://www.rubyist.net/~slagell/ruby/strings.html String] in Ruby may be double-quoted (&amp;quot;...&amp;quot;) or single-quoted ('...'). &lt;br /&gt;
&lt;br /&gt;
However, double-quoting and single-quoting have different effects in some cases. A double-quoted string allows character escapes by a leading backslash, and the evaluation of embedded expressions using #{}. A single-quoted string does not interpret this. This is shown in the example below:&lt;br /&gt;
&lt;br /&gt;
  string1 = &amp;quot;abc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;abc&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  string2 = 'efg'&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;a\nb\nc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;a&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
  &amp;gt;&amp;gt;c&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts 'a\nb\n'&lt;br /&gt;
  &amp;gt;&amp;gt;a\nb\nc&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;abcd #{5*3} efg&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;abcd 15 efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts var = &amp;quot; abc &amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot; abc &amp;quot;&lt;br /&gt;
  puts &amp;quot;1234#{var}5678&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;1234 abc 5678&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby strings can be concatenated using + operator. A string can be repeated multiple times using * operator. The example below shows the use of + and * operators. Ruby string handling is more powerful than C. Since Ruby, provides support for regular expressions, string handling and pattern matching can be made very efficiently and easily. &lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; + &amp;quot;bar&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foobar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; * 2&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foofoo&amp;quot;&lt;br /&gt;
&lt;br /&gt;
===Symbols===&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols] in Ruby are Strings, just with an important difference, Symbols are immutable while Strings are not. Mutable objects can be changed after assignment while immutable objects can only be overwritten. Ruby is quite unique in offering mutable Strings, which adds greatly to its expressiveness. However mutable Strings can have their share of issues in terms of creating unexpected results and reduced performance. It is for this reason Ruby also offers programmers the choice of Symbols.&lt;br /&gt;
&lt;br /&gt;
Symbols need not contain only alphanumeric characters, by using quotes you can not only use the same characters as you would in a String, but also interpolate Symbols as well. Syntactically, a Symbol needs a colon : prepended. Symbols are actually so similar to Strings that converting between the two is very simple and consistent.&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello&amp;quot;&lt;br /&gt;
  :hello&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  bang = &amp;quot;!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; &amp;quot;hello world!&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; :&amp;quot;hello world!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ This] and [http://www.troubleshooters.com/codecorn/ruby/symbols.htm This] delve deeply into the difference between string and symbol. Well they are interesting worth reading atleast once.&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash] is a collection of Key-Value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.&lt;br /&gt;
&lt;br /&gt;
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. There are three different ways to create a Hashes. &lt;br /&gt;
&lt;br /&gt;
1. Using the keyword  '''new'''.&lt;br /&gt;
  &lt;br /&gt;
  z = Hash.new&lt;br /&gt;
  z[&amp;quot;a&amp;quot;] = 'Hello'&lt;br /&gt;
  z[&amp;quot;b&amp;quot;] = 'Hi'&lt;br /&gt;
  z[&amp;quot;c&amp;quot;] = 'Hey'&lt;br /&gt;
  &lt;br /&gt;
  puts z['a']&lt;br /&gt;
  &amp;gt;&amp;gt;Hello&lt;br /&gt;
  &lt;br /&gt;
  puts z['b']&lt;br /&gt;
  &amp;gt;&amp;gt;Hi&lt;br /&gt;
  &lt;br /&gt;
  puts z['c']&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
2. Using special syntax  '''Hash[ ]'''. &lt;br /&gt;
  &lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Block] is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
This example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you don't have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambdas in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterator] is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closure] in Ruby is a block that closes over. Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x). More about currying may be learnt from [http://en.wikipedia.org/wiki/Currying here]. Currying is a very interesting feature and together with closures, it provides a flexibility to ruby at ease which is not easily achievable in java or c++.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language that provides easy programming. The features of Ruby have made it a very popular and extensible language.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/ &amp;quot;Programming Ruby - The Pragmatic Programmer's Guide&amp;quot;]&amp;lt;br&amp;gt; &lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Object Oriented Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-lang.org/en/about/ Features of Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closures]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterators in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules and Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Duck_typing Duck Typing - Unbounded Polymorphism]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html Regular expressions]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interactive_Ruby_Shell Interactive Ruby Browser]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Array.html Array class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/strings.html String]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Blocks, Procs and Lambdas]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.csc.ncsu.edu/faculty/efg/517/f11/schedule CSC517 Class notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51959</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51959"/>
		<updated>2011-10-15T00:37:25Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Symbols */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamic], [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 reflective], [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented], [http://en.wikipedia.org/wiki/Interpreted_language single-pass interpreted] programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports [http://en.wikipedia.org/wiki/Functional_programming functional], [http://en.wikipedia.org/wiki/Imperative_programming imperative], reflective, object oriented and many other programming paradigms. Since Ruby was designed to incorporate the best features of scripting and Object oriented languages, Ruby emerged to be a very powerful language with native support for many features that are not in the 'nature' of other languages like Java and C++. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro &amp;quot;Matz&amp;quot; Matsumoto]. The intent of developing Ruby was to have a new language that balances functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than [http://www.perl.org/ Perl] and more Object Oriented than [http://www.python.org/ Python].&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of [http://en.wikipedia.org/wiki/Ruby_on_rails Ruby on Rails] in 2005, an [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, [http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html TIOBE programming language popularity index] state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. [http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Purely object oriented]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. [http://c2.com/cgi/wiki?DynamicTyping Dynamically typed]'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Blocks and Closures]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. [http://www.rubyist.net/~slagell/ruby/iterators.html Built in Iterators]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Multiple Inheritance through Mixins]'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. [http://en.wikipedia.org/wiki/Duck_typing Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;]'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. [http://www.regular-expressions.info/ruby.html Syntax level Perl compatible for regular expressions]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. [http://designpatternsinruby.com/ Built-in support for certain Design Patterns]'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. [http://corelib.rubyonrails.org/classes/GC.html Mark-and-Sweep garbage collection]'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. [http://www.ruby-doc.org/core-1.9.2/Thread.html OS independent threading]'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. [http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Easy interface to C modules]'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. [http://www.ruby-lang.org/en/about/ Portable easily]'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
Here is a little table that highlights the key differences between java and ruby. Many of these exact difference maybe shared by Ruby and other languages like C,C++ etc.&amp;lt;br&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing. &lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Methods are passed to the objects&lt;br /&gt;
| Objects are passed to the methods&lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the [http://en.wikipedia.org/wiki/Interactive_Ruby_Shell IRB]. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by [http://www.eclipse.org/ Eclipse]. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
[http://www.jetbrains.com/ruby/ RubyMine] is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
The ''.rb'' extension is used to save Ruby files. The Ruby programs can be run by the command ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file. The path to the ruby command is specified here.&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
===Functions===&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. Ruby does not use { } parenthesis to specify scope. Instead, the keyword  '''end''' is equivalent to the closing parenthesis.&lt;br /&gt;
&lt;br /&gt;
  def fivetimes(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  fivetimes&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized in the function definition itself. The arguments can be specified without ( ) parentheses. This means parentheses in a function call are optional. We can write  ''fivetimes ( 4 )''  or  ''fivetimes 4'' . &lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greeting(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greeting('Sam', 'Peter', 'Derek')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Sam, Peter, Derek&lt;br /&gt;
&lt;br /&gt;
The user need not specify any return value or have an explicit ''return'' for a function.&lt;br /&gt;
The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object.&amp;lt;br&amp;gt;[http://rubylearning.com/satishtalim/writing_own_ruby_methods.html This] is really helpful to learn more about methods in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
One of the reasons that makes Ruby easier to program is the fact that the programmer need not specify the type for any variable. Variables in Ruby are not given a type. Their “type” depends upon the value assigned to them. This feature of Ruby is called [http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing].&lt;br /&gt;
&lt;br /&gt;
In the examples below, the variables ''PI'', ''a'', ''b'', ''c'' are not given a type by the programmer. At run time, ''PI'' is assigned type ''float'', ''a'' and ''b'' are assigned type ''int'', and ''c'' is assigned type ''array''. &lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  ''#''  and run to the end of the line. &lt;br /&gt;
&lt;br /&gt;
Dynamic typing makes many features like unbounded polymorphism, blocks and closures possible. These features are not available in statically typed languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers, strings and arrays are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;changetoupcase&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;CHANGETOUPCASE&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
  &lt;br /&gt;
  [1,3,6,8].class&lt;br /&gt;
  &amp;gt;&amp;gt;Array&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.sitepoint.com/typing-versus-dynamic-typing/ Here] is something we got off the internet which explains Dynamic typing well although its not particular to Ruby.&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An [http://www.ruby-doc.org/core-1.9.2/Array.html array] in Ruby is an ordered list of elements. An array can be considered an object of objects. In C, C++, Java, C# and other languages, an array consists of elements of a similar type. In Ruby, there is no such constraint. An array can consist of any object. We can have a single array that has Fixnum, String, Array etc as elements. &lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
  d = ['I am string', 4.5, 1000000, [1,2,3,4]] # elements of different type&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array is illustrated below:&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
  &lt;br /&gt;
  b &amp;lt;&amp;lt; [10,100]&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4, [10, 100]]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  ''b[‐1]''  refers to the last element of  ''b''. &lt;br /&gt;
For example, consider the array ''b'' above.&lt;br /&gt;
&lt;br /&gt;
  puts b[-1]&lt;br /&gt;
  &amp;gt;&amp;gt;[10, 100]&lt;br /&gt;
  &lt;br /&gt;
  puts b[-2]&lt;br /&gt;
  &amp;gt;&amp;gt;4&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][0]&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][1]&lt;br /&gt;
  &amp;gt;&amp;gt;100 &lt;br /&gt;
&lt;br /&gt;
Arrays can also be created using the  ''new''  keyword. ''new''  is used to create objects of any type. &lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Strings==&lt;br /&gt;
&lt;br /&gt;
A [http://www.rubyist.net/~slagell/ruby/strings.html String] in Ruby may be double-quoted (&amp;quot;...&amp;quot;) or single-quoted ('...'). &lt;br /&gt;
&lt;br /&gt;
However, double-quoting and single-quoting have different effects in some cases. A double-quoted string allows character escapes by a leading backslash, and the evaluation of embedded expressions using #{}. A single-quoted string does not interpret this. This is shown in the example below:&lt;br /&gt;
&lt;br /&gt;
  string1 = &amp;quot;abc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;abc&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  string2 = 'efg'&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;a\nb\nc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;a&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
  &amp;gt;&amp;gt;c&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts 'a\nb\n'&lt;br /&gt;
  &amp;gt;&amp;gt;a\nb\nc&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;abcd #{5*3} efg&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;abcd 15 efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts var = &amp;quot; abc &amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot; abc &amp;quot;&lt;br /&gt;
  puts &amp;quot;1234#{var}5678&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;1234 abc 5678&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby strings can be concatenated using + operator. A string can be repeated multiple times using * operator. The example below shows the use of + and * operators. Ruby string handling is more powerful than C. Since Ruby, provides support for regular expressions, string handling and pattern matching can be made very efficiently and easily. &lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; + &amp;quot;bar&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foobar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; * 2&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foofoo&amp;quot;&lt;br /&gt;
&lt;br /&gt;
===Symbols===&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols] in Ruby are Strings, just with an important difference, Symbols are immutable while Strings are not. Mutable objects can be changed after assignment while immutable objects can only be overwritten. Ruby is quite unique in offering mutable Strings, which adds greatly to its expressiveness. However mutable Strings can have their share of issues in terms of creating unexpected results and reduced performance. It is for this reason Ruby also offers programmers the choice of Symbols.&lt;br /&gt;
&lt;br /&gt;
Symbols need not contain only alphanumeric characters, by using quotes you can not only use the same characters as you would in a String, but also interpolate Symbols as well. Syntactically, a Symbol needs a colon : prepended. Symbols are actually so similar to Strings that converting between the two is very simple and consistent.&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello&amp;quot;&lt;br /&gt;
  :hello&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  bang = &amp;quot;!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; &amp;quot;hello world!&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; :&amp;quot;hello world!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ This] and [http://www.troubleshooters.com/codecorn/ruby/symbols.htm This] delve deeply into the difference between string and symbol. Well they are worth a read.&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash] is a collection of Key-Value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.&lt;br /&gt;
&lt;br /&gt;
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. There are three different ways to create a Hashes. &lt;br /&gt;
&lt;br /&gt;
1. Using the keyword  '''new'''.&lt;br /&gt;
  &lt;br /&gt;
  z = Hash.new&lt;br /&gt;
  z[&amp;quot;a&amp;quot;] = 'Hello'&lt;br /&gt;
  z[&amp;quot;b&amp;quot;] = 'Hi'&lt;br /&gt;
  z[&amp;quot;c&amp;quot;] = 'Hey'&lt;br /&gt;
  &lt;br /&gt;
  puts z['a']&lt;br /&gt;
  &amp;gt;&amp;gt;Hello&lt;br /&gt;
  &lt;br /&gt;
  puts z['b']&lt;br /&gt;
  &amp;gt;&amp;gt;Hi&lt;br /&gt;
  &lt;br /&gt;
  puts z['c']&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
2. Using special syntax  '''Hash[ ]'''. &lt;br /&gt;
  &lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Block] is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
This example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you don't have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambdas in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterator] is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closure] in Ruby is a block that closes over. Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x). More about currying may be learnt from [http://en.wikipedia.org/wiki/Currying here]. Currying is a very interesting feature and together with closures, it provides a flexibility to ruby at ease which is not easily achievable in java or c++.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language that provides easy programming. The features of Ruby have made it a very popular and extensible language.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/ &amp;quot;Programming Ruby - The Pragmatic Programmer's Guide&amp;quot;]&amp;lt;br&amp;gt; &lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Object Oriented Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-lang.org/en/about/ Features of Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closures]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterators in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules and Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Duck_typing Duck Typing - Unbounded Polymorphism]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html Regular expressions]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interactive_Ruby_Shell Interactive Ruby Browser]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Array.html Array class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/strings.html String]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Blocks, Procs and Lambdas]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.csc.ncsu.edu/faculty/efg/517/f11/schedule CSC517 Class notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51958</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51958"/>
		<updated>2011-10-15T00:34:04Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Dynamic Typing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamic], [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 reflective], [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented], [http://en.wikipedia.org/wiki/Interpreted_language single-pass interpreted] programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports [http://en.wikipedia.org/wiki/Functional_programming functional], [http://en.wikipedia.org/wiki/Imperative_programming imperative], reflective, object oriented and many other programming paradigms. Since Ruby was designed to incorporate the best features of scripting and Object oriented languages, Ruby emerged to be a very powerful language with native support for many features that are not in the 'nature' of other languages like Java and C++. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro &amp;quot;Matz&amp;quot; Matsumoto]. The intent of developing Ruby was to have a new language that balances functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than [http://www.perl.org/ Perl] and more Object Oriented than [http://www.python.org/ Python].&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of [http://en.wikipedia.org/wiki/Ruby_on_rails Ruby on Rails] in 2005, an [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, [http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html TIOBE programming language popularity index] state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. [http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Purely object oriented]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. [http://c2.com/cgi/wiki?DynamicTyping Dynamically typed]'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Blocks and Closures]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. [http://www.rubyist.net/~slagell/ruby/iterators.html Built in Iterators]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Multiple Inheritance through Mixins]'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. [http://en.wikipedia.org/wiki/Duck_typing Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;]'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. [http://www.regular-expressions.info/ruby.html Syntax level Perl compatible for regular expressions]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. [http://designpatternsinruby.com/ Built-in support for certain Design Patterns]'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. [http://corelib.rubyonrails.org/classes/GC.html Mark-and-Sweep garbage collection]'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. [http://www.ruby-doc.org/core-1.9.2/Thread.html OS independent threading]'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. [http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Easy interface to C modules]'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. [http://www.ruby-lang.org/en/about/ Portable easily]'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
Here is a little table that highlights the key differences between java and ruby. Many of these exact difference maybe shared by Ruby and other languages like C,C++ etc.&amp;lt;br&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing. &lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Methods are passed to the objects&lt;br /&gt;
| Objects are passed to the methods&lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the [http://en.wikipedia.org/wiki/Interactive_Ruby_Shell IRB]. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by [http://www.eclipse.org/ Eclipse]. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
[http://www.jetbrains.com/ruby/ RubyMine] is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
The ''.rb'' extension is used to save Ruby files. The Ruby programs can be run by the command ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file. The path to the ruby command is specified here.&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
===Functions===&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. Ruby does not use { } parenthesis to specify scope. Instead, the keyword  '''end''' is equivalent to the closing parenthesis.&lt;br /&gt;
&lt;br /&gt;
  def fivetimes(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  fivetimes&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized in the function definition itself. The arguments can be specified without ( ) parentheses. This means parentheses in a function call are optional. We can write  ''fivetimes ( 4 )''  or  ''fivetimes 4'' . &lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greeting(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greeting('Sam', 'Peter', 'Derek')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Sam, Peter, Derek&lt;br /&gt;
&lt;br /&gt;
The user need not specify any return value or have an explicit ''return'' for a function.&lt;br /&gt;
The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object.&amp;lt;br&amp;gt;[http://rubylearning.com/satishtalim/writing_own_ruby_methods.html This] is really helpful to learn more about methods in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
One of the reasons that makes Ruby easier to program is the fact that the programmer need not specify the type for any variable. Variables in Ruby are not given a type. Their “type” depends upon the value assigned to them. This feature of Ruby is called [http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing].&lt;br /&gt;
&lt;br /&gt;
In the examples below, the variables ''PI'', ''a'', ''b'', ''c'' are not given a type by the programmer. At run time, ''PI'' is assigned type ''float'', ''a'' and ''b'' are assigned type ''int'', and ''c'' is assigned type ''array''. &lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  ''#''  and run to the end of the line. &lt;br /&gt;
&lt;br /&gt;
Dynamic typing makes many features like unbounded polymorphism, blocks and closures possible. These features are not available in statically typed languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers, strings and arrays are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;changetoupcase&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;CHANGETOUPCASE&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
  &lt;br /&gt;
  [1,3,6,8].class&lt;br /&gt;
  &amp;gt;&amp;gt;Array&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.sitepoint.com/typing-versus-dynamic-typing/ Here] is something we got off the internet which explains Dynamic typing well although its not particular to Ruby.&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An [http://www.ruby-doc.org/core-1.9.2/Array.html array] in Ruby is an ordered list of elements. An array can be considered an object of objects. In C, C++, Java, C# and other languages, an array consists of elements of a similar type. In Ruby, there is no such constraint. An array can consist of any object. We can have a single array that has Fixnum, String, Array etc as elements. &lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
  d = ['I am string', 4.5, 1000000, [1,2,3,4]] # elements of different type&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array is illustrated below:&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
  &lt;br /&gt;
  b &amp;lt;&amp;lt; [10,100]&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4, [10, 100]]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  ''b[‐1]''  refers to the last element of  ''b''. &lt;br /&gt;
For example, consider the array ''b'' above.&lt;br /&gt;
&lt;br /&gt;
  puts b[-1]&lt;br /&gt;
  &amp;gt;&amp;gt;[10, 100]&lt;br /&gt;
  &lt;br /&gt;
  puts b[-2]&lt;br /&gt;
  &amp;gt;&amp;gt;4&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][0]&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][1]&lt;br /&gt;
  &amp;gt;&amp;gt;100 &lt;br /&gt;
&lt;br /&gt;
Arrays can also be created using the  ''new''  keyword. ''new''  is used to create objects of any type. &lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Strings==&lt;br /&gt;
&lt;br /&gt;
A [http://www.rubyist.net/~slagell/ruby/strings.html String] in Ruby may be double-quoted (&amp;quot;...&amp;quot;) or single-quoted ('...'). &lt;br /&gt;
&lt;br /&gt;
However, double-quoting and single-quoting have different effects in some cases. A double-quoted string allows character escapes by a leading backslash, and the evaluation of embedded expressions using #{}. A single-quoted string does not interpret this. This is shown in the example below:&lt;br /&gt;
&lt;br /&gt;
  string1 = &amp;quot;abc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;abc&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  string2 = 'efg'&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;a\nb\nc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;a&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
  &amp;gt;&amp;gt;c&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts 'a\nb\n'&lt;br /&gt;
  &amp;gt;&amp;gt;a\nb\nc&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;abcd #{5*3} efg&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;abcd 15 efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts var = &amp;quot; abc &amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot; abc &amp;quot;&lt;br /&gt;
  puts &amp;quot;1234#{var}5678&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;1234 abc 5678&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby strings can be concatenated using + operator. A string can be repeated multiple times using * operator. The example below shows the use of + and * operators. Ruby string handling is more powerful than C. Since Ruby, provides support for regular expressions, string handling and pattern matching can be made very efficiently and easily. &lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; + &amp;quot;bar&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foobar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; * 2&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foofoo&amp;quot;&lt;br /&gt;
&lt;br /&gt;
===Symbols===&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols] in Ruby are Strings, just with an important difference, Symbols are immutable while Strings are not. Mutable objects can be changed after assignment while immutable objects can only be overwritten. Ruby is quite unique in offering mutable Strings, which adds greatly to its expressiveness. However mutable Strings can have their share of issues in terms of creating unexpected results and reduced performance. It is for this reason Ruby also offers programmers the choice of Symbols.&lt;br /&gt;
&lt;br /&gt;
Symbols need not contain only alphanumeric characters, by using quotes you can not only use the same characters as you would in a String, but also interpolate Symbols as well. Syntactically, a Symbol needs a colon : prepended. Symbols are actually so similar to Strings that converting between the two is very simple and consistent.&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello&amp;quot;&lt;br /&gt;
  :hello&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  bang = &amp;quot;!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; &amp;quot;hello world!&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; :&amp;quot;hello world!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash] is a collection of Key-Value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.&lt;br /&gt;
&lt;br /&gt;
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. There are three different ways to create a Hashes. &lt;br /&gt;
&lt;br /&gt;
1. Using the keyword  '''new'''.&lt;br /&gt;
  &lt;br /&gt;
  z = Hash.new&lt;br /&gt;
  z[&amp;quot;a&amp;quot;] = 'Hello'&lt;br /&gt;
  z[&amp;quot;b&amp;quot;] = 'Hi'&lt;br /&gt;
  z[&amp;quot;c&amp;quot;] = 'Hey'&lt;br /&gt;
  &lt;br /&gt;
  puts z['a']&lt;br /&gt;
  &amp;gt;&amp;gt;Hello&lt;br /&gt;
  &lt;br /&gt;
  puts z['b']&lt;br /&gt;
  &amp;gt;&amp;gt;Hi&lt;br /&gt;
  &lt;br /&gt;
  puts z['c']&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
2. Using special syntax  '''Hash[ ]'''. &lt;br /&gt;
  &lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Block] is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
This example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you don't have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambdas in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterator] is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closure] in Ruby is a block that closes over. Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x). More about currying may be learnt from [http://en.wikipedia.org/wiki/Currying here]. Currying is a very interesting feature and together with closures, it provides a flexibility to ruby at ease which is not easily achievable in java or c++.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language that provides easy programming. The features of Ruby have made it a very popular and extensible language.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/ &amp;quot;Programming Ruby - The Pragmatic Programmer's Guide&amp;quot;]&amp;lt;br&amp;gt; &lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Object Oriented Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-lang.org/en/about/ Features of Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closures]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterators in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules and Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Duck_typing Duck Typing - Unbounded Polymorphism]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html Regular expressions]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interactive_Ruby_Shell Interactive Ruby Browser]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Array.html Array class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/strings.html String]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Blocks, Procs and Lambdas]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.csc.ncsu.edu/faculty/efg/517/f11/schedule CSC517 Class notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51957</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51957"/>
		<updated>2011-10-15T00:33:47Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Dynamic Typing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamic], [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 reflective], [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented], [http://en.wikipedia.org/wiki/Interpreted_language single-pass interpreted] programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports [http://en.wikipedia.org/wiki/Functional_programming functional], [http://en.wikipedia.org/wiki/Imperative_programming imperative], reflective, object oriented and many other programming paradigms. Since Ruby was designed to incorporate the best features of scripting and Object oriented languages, Ruby emerged to be a very powerful language with native support for many features that are not in the 'nature' of other languages like Java and C++. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro &amp;quot;Matz&amp;quot; Matsumoto]. The intent of developing Ruby was to have a new language that balances functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than [http://www.perl.org/ Perl] and more Object Oriented than [http://www.python.org/ Python].&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of [http://en.wikipedia.org/wiki/Ruby_on_rails Ruby on Rails] in 2005, an [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, [http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html TIOBE programming language popularity index] state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. [http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Purely object oriented]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. [http://c2.com/cgi/wiki?DynamicTyping Dynamically typed]'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Blocks and Closures]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. [http://www.rubyist.net/~slagell/ruby/iterators.html Built in Iterators]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Multiple Inheritance through Mixins]'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. [http://en.wikipedia.org/wiki/Duck_typing Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;]'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. [http://www.regular-expressions.info/ruby.html Syntax level Perl compatible for regular expressions]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. [http://designpatternsinruby.com/ Built-in support for certain Design Patterns]'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. [http://corelib.rubyonrails.org/classes/GC.html Mark-and-Sweep garbage collection]'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. [http://www.ruby-doc.org/core-1.9.2/Thread.html OS independent threading]'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. [http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Easy interface to C modules]'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. [http://www.ruby-lang.org/en/about/ Portable easily]'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
Here is a little table that highlights the key differences between java and ruby. Many of these exact difference maybe shared by Ruby and other languages like C,C++ etc.&amp;lt;br&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing. &lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Methods are passed to the objects&lt;br /&gt;
| Objects are passed to the methods&lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the [http://en.wikipedia.org/wiki/Interactive_Ruby_Shell IRB]. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by [http://www.eclipse.org/ Eclipse]. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
[http://www.jetbrains.com/ruby/ RubyMine] is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
The ''.rb'' extension is used to save Ruby files. The Ruby programs can be run by the command ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file. The path to the ruby command is specified here.&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
===Functions===&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. Ruby does not use { } parenthesis to specify scope. Instead, the keyword  '''end''' is equivalent to the closing parenthesis.&lt;br /&gt;
&lt;br /&gt;
  def fivetimes(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  fivetimes&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized in the function definition itself. The arguments can be specified without ( ) parentheses. This means parentheses in a function call are optional. We can write  ''fivetimes ( 4 )''  or  ''fivetimes 4'' . &lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greeting(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greeting('Sam', 'Peter', 'Derek')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Sam, Peter, Derek&lt;br /&gt;
&lt;br /&gt;
The user need not specify any return value or have an explicit ''return'' for a function.&lt;br /&gt;
The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object.&amp;lt;br&amp;gt;[http://rubylearning.com/satishtalim/writing_own_ruby_methods.html This] is really helpful to learn more about methods in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
One of the reasons that makes Ruby easier to program is the fact that the programmer need not specify the type for any variable. Variables in Ruby are not given a type. Their “type” depends upon the value assigned to them. This feature of Ruby is called [http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing].&lt;br /&gt;
&lt;br /&gt;
In the examples below, the variables ''PI'', ''a'', ''b'', ''c'' are not given a type by the programmer. At run time, ''PI'' is assigned type ''float'', ''a'' and ''b'' are assigned type ''int'', and ''c'' is assigned type ''array''. &lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  ''#''  and run to the end of the line. &lt;br /&gt;
&lt;br /&gt;
Dynamic typing makes many features like unbounded polymorphism, blocks and closures possible. These features are not available in statically typed languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers, strings and arrays are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;changetoupcase&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;CHANGETOUPCASE&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
  &lt;br /&gt;
  [1,3,6,8].class&lt;br /&gt;
  &amp;gt;&amp;gt;Array&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.sitepoint.com/typing-versus-dynamic-typing/ Here] is something we got off the internet which explains Dynamic typing well altough its not particular to Ruby.&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An [http://www.ruby-doc.org/core-1.9.2/Array.html array] in Ruby is an ordered list of elements. An array can be considered an object of objects. In C, C++, Java, C# and other languages, an array consists of elements of a similar type. In Ruby, there is no such constraint. An array can consist of any object. We can have a single array that has Fixnum, String, Array etc as elements. &lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
  d = ['I am string', 4.5, 1000000, [1,2,3,4]] # elements of different type&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array is illustrated below:&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
  &lt;br /&gt;
  b &amp;lt;&amp;lt; [10,100]&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4, [10, 100]]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  ''b[‐1]''  refers to the last element of  ''b''. &lt;br /&gt;
For example, consider the array ''b'' above.&lt;br /&gt;
&lt;br /&gt;
  puts b[-1]&lt;br /&gt;
  &amp;gt;&amp;gt;[10, 100]&lt;br /&gt;
  &lt;br /&gt;
  puts b[-2]&lt;br /&gt;
  &amp;gt;&amp;gt;4&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][0]&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][1]&lt;br /&gt;
  &amp;gt;&amp;gt;100 &lt;br /&gt;
&lt;br /&gt;
Arrays can also be created using the  ''new''  keyword. ''new''  is used to create objects of any type. &lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Strings==&lt;br /&gt;
&lt;br /&gt;
A [http://www.rubyist.net/~slagell/ruby/strings.html String] in Ruby may be double-quoted (&amp;quot;...&amp;quot;) or single-quoted ('...'). &lt;br /&gt;
&lt;br /&gt;
However, double-quoting and single-quoting have different effects in some cases. A double-quoted string allows character escapes by a leading backslash, and the evaluation of embedded expressions using #{}. A single-quoted string does not interpret this. This is shown in the example below:&lt;br /&gt;
&lt;br /&gt;
  string1 = &amp;quot;abc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;abc&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  string2 = 'efg'&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;a\nb\nc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;a&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
  &amp;gt;&amp;gt;c&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts 'a\nb\n'&lt;br /&gt;
  &amp;gt;&amp;gt;a\nb\nc&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;abcd #{5*3} efg&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;abcd 15 efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts var = &amp;quot; abc &amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot; abc &amp;quot;&lt;br /&gt;
  puts &amp;quot;1234#{var}5678&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;1234 abc 5678&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby strings can be concatenated using + operator. A string can be repeated multiple times using * operator. The example below shows the use of + and * operators. Ruby string handling is more powerful than C. Since Ruby, provides support for regular expressions, string handling and pattern matching can be made very efficiently and easily. &lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; + &amp;quot;bar&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foobar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; * 2&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foofoo&amp;quot;&lt;br /&gt;
&lt;br /&gt;
===Symbols===&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols] in Ruby are Strings, just with an important difference, Symbols are immutable while Strings are not. Mutable objects can be changed after assignment while immutable objects can only be overwritten. Ruby is quite unique in offering mutable Strings, which adds greatly to its expressiveness. However mutable Strings can have their share of issues in terms of creating unexpected results and reduced performance. It is for this reason Ruby also offers programmers the choice of Symbols.&lt;br /&gt;
&lt;br /&gt;
Symbols need not contain only alphanumeric characters, by using quotes you can not only use the same characters as you would in a String, but also interpolate Symbols as well. Syntactically, a Symbol needs a colon : prepended. Symbols are actually so similar to Strings that converting between the two is very simple and consistent.&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello&amp;quot;&lt;br /&gt;
  :hello&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  bang = &amp;quot;!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; &amp;quot;hello world!&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; :&amp;quot;hello world!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash] is a collection of Key-Value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.&lt;br /&gt;
&lt;br /&gt;
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. There are three different ways to create a Hashes. &lt;br /&gt;
&lt;br /&gt;
1. Using the keyword  '''new'''.&lt;br /&gt;
  &lt;br /&gt;
  z = Hash.new&lt;br /&gt;
  z[&amp;quot;a&amp;quot;] = 'Hello'&lt;br /&gt;
  z[&amp;quot;b&amp;quot;] = 'Hi'&lt;br /&gt;
  z[&amp;quot;c&amp;quot;] = 'Hey'&lt;br /&gt;
  &lt;br /&gt;
  puts z['a']&lt;br /&gt;
  &amp;gt;&amp;gt;Hello&lt;br /&gt;
  &lt;br /&gt;
  puts z['b']&lt;br /&gt;
  &amp;gt;&amp;gt;Hi&lt;br /&gt;
  &lt;br /&gt;
  puts z['c']&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
2. Using special syntax  '''Hash[ ]'''. &lt;br /&gt;
  &lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Block] is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
This example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you don't have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambdas in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterator] is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closure] in Ruby is a block that closes over. Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x). More about currying may be learnt from [http://en.wikipedia.org/wiki/Currying here]. Currying is a very interesting feature and together with closures, it provides a flexibility to ruby at ease which is not easily achievable in java or c++.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language that provides easy programming. The features of Ruby have made it a very popular and extensible language.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/ &amp;quot;Programming Ruby - The Pragmatic Programmer's Guide&amp;quot;]&amp;lt;br&amp;gt; &lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Object Oriented Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-lang.org/en/about/ Features of Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closures]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterators in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules and Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Duck_typing Duck Typing - Unbounded Polymorphism]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html Regular expressions]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interactive_Ruby_Shell Interactive Ruby Browser]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Array.html Array class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/strings.html String]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Blocks, Procs and Lambdas]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.csc.ncsu.edu/faculty/efg/517/f11/schedule CSC517 Class notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51956</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51956"/>
		<updated>2011-10-15T00:23:05Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamic], [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 reflective], [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented], [http://en.wikipedia.org/wiki/Interpreted_language single-pass interpreted] programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports [http://en.wikipedia.org/wiki/Functional_programming functional], [http://en.wikipedia.org/wiki/Imperative_programming imperative], reflective, object oriented and many other programming paradigms. Since Ruby was designed to incorporate the best features of scripting and Object oriented languages, Ruby emerged to be a very powerful language with native support for many features that are not in the 'nature' of other languages like Java and C++. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro &amp;quot;Matz&amp;quot; Matsumoto]. The intent of developing Ruby was to have a new language that balances functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than [http://www.perl.org/ Perl] and more Object Oriented than [http://www.python.org/ Python].&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of [http://en.wikipedia.org/wiki/Ruby_on_rails Ruby on Rails] in 2005, an [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, [http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html TIOBE programming language popularity index] state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. [http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Purely object oriented]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. [http://c2.com/cgi/wiki?DynamicTyping Dynamically typed]'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Blocks and Closures]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. [http://www.rubyist.net/~slagell/ruby/iterators.html Built in Iterators]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Multiple Inheritance through Mixins]'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. [http://en.wikipedia.org/wiki/Duck_typing Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;]'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. [http://www.regular-expressions.info/ruby.html Syntax level Perl compatible for regular expressions]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. [http://designpatternsinruby.com/ Built-in support for certain Design Patterns]'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. [http://corelib.rubyonrails.org/classes/GC.html Mark-and-Sweep garbage collection]'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. [http://www.ruby-doc.org/core-1.9.2/Thread.html OS independent threading]'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. [http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Easy interface to C modules]'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. [http://www.ruby-lang.org/en/about/ Portable easily]'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
Here is a little table that highlights the key differences between java and ruby. Many of these exact difference maybe shared by Ruby and other languages like C,C++ etc.&amp;lt;br&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing. &lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Methods are passed to the objects&lt;br /&gt;
| Objects are passed to the methods&lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the [http://en.wikipedia.org/wiki/Interactive_Ruby_Shell IRB]. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by [http://www.eclipse.org/ Eclipse]. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
[http://www.jetbrains.com/ruby/ RubyMine] is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
The ''.rb'' extension is used to save Ruby files. The Ruby programs can be run by the command ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file. The path to the ruby command is specified here.&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
===Functions===&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. Ruby does not use { } parenthesis to specify scope. Instead, the keyword  '''end''' is equivalent to the closing parenthesis.&lt;br /&gt;
&lt;br /&gt;
  def fivetimes(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  fivetimes&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized in the function definition itself. The arguments can be specified without ( ) parentheses. This means parentheses in a function call are optional. We can write  ''fivetimes ( 4 )''  or  ''fivetimes 4'' . &lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greeting(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greeting('Sam', 'Peter', 'Derek')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Sam, Peter, Derek&lt;br /&gt;
&lt;br /&gt;
The user need not specify any return value or have an explicit ''return'' for a function.&lt;br /&gt;
The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object.&amp;lt;br&amp;gt;[http://rubylearning.com/satishtalim/writing_own_ruby_methods.html This] is really helpful to learn more about methods in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
One of the reasons that makes Ruby easier to program is the fact that the programmer need not specify the type for any variable. Variables in Ruby are not given a type. Their “type” depends upon the value assigned to them. This feature of Ruby is called [http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing].&lt;br /&gt;
&lt;br /&gt;
In the examples below, the variables ''PI'', ''a'', ''b'', ''c'' are not given a type by the programmer. At run time, ''PI'' is assigned type ''float'', ''a'' and ''b'' are assigned type ''int'', and ''c'' is assigned type ''array''. &lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  ''#''  and run to the end of the line. &lt;br /&gt;
&lt;br /&gt;
Dynamic typing makes many features like unbounded polymorphism, blocks and closures possible. These features are not available in statically typed languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers, strings and arrays are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;changetoupcase&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;CHANGETOUPCASE&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
  &lt;br /&gt;
  [1,3,6,8].class&lt;br /&gt;
  &amp;gt;&amp;gt;Array&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An [http://www.ruby-doc.org/core-1.9.2/Array.html array] in Ruby is an ordered list of elements. An array can be considered an object of objects. In C, C++, Java, C# and other languages, an array consists of elements of a similar type. In Ruby, there is no such constraint. An array can consist of any object. We can have a single array that has Fixnum, String, Array etc as elements. &lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
  d = ['I am string', 4.5, 1000000, [1,2,3,4]] # elements of different type&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array is illustrated below:&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
  &lt;br /&gt;
  b &amp;lt;&amp;lt; [10,100]&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4, [10, 100]]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  ''b[‐1]''  refers to the last element of  ''b''. &lt;br /&gt;
For example, consider the array ''b'' above.&lt;br /&gt;
&lt;br /&gt;
  puts b[-1]&lt;br /&gt;
  &amp;gt;&amp;gt;[10, 100]&lt;br /&gt;
  &lt;br /&gt;
  puts b[-2]&lt;br /&gt;
  &amp;gt;&amp;gt;4&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][0]&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][1]&lt;br /&gt;
  &amp;gt;&amp;gt;100 &lt;br /&gt;
&lt;br /&gt;
Arrays can also be created using the  ''new''  keyword. ''new''  is used to create objects of any type. &lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Strings==&lt;br /&gt;
&lt;br /&gt;
A [http://www.rubyist.net/~slagell/ruby/strings.html String] in Ruby may be double-quoted (&amp;quot;...&amp;quot;) or single-quoted ('...'). &lt;br /&gt;
&lt;br /&gt;
However, double-quoting and single-quoting have different effects in some cases. A double-quoted string allows character escapes by a leading backslash, and the evaluation of embedded expressions using #{}. A single-quoted string does not interpret this. This is shown in the example below:&lt;br /&gt;
&lt;br /&gt;
  string1 = &amp;quot;abc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;abc&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  string2 = 'efg'&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;a\nb\nc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;a&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
  &amp;gt;&amp;gt;c&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts 'a\nb\n'&lt;br /&gt;
  &amp;gt;&amp;gt;a\nb\nc&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;abcd #{5*3} efg&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;abcd 15 efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts var = &amp;quot; abc &amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot; abc &amp;quot;&lt;br /&gt;
  puts &amp;quot;1234#{var}5678&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;1234 abc 5678&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby strings can be concatenated using + operator. A string can be repeated multiple times using * operator. The example below shows the use of + and * operators. Ruby string handling is more powerful than C. Since Ruby, provides support for regular expressions, string handling and pattern matching can be made very efficiently and easily. &lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; + &amp;quot;bar&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foobar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; * 2&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foofoo&amp;quot;&lt;br /&gt;
&lt;br /&gt;
===Symbols===&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols] in Ruby are Strings, just with an important difference, Symbols are immutable while Strings are not. Mutable objects can be changed after assignment while immutable objects can only be overwritten. Ruby is quite unique in offering mutable Strings, which adds greatly to its expressiveness. However mutable Strings can have their share of issues in terms of creating unexpected results and reduced performance. It is for this reason Ruby also offers programmers the choice of Symbols.&lt;br /&gt;
&lt;br /&gt;
Symbols need not contain only alphanumeric characters, by using quotes you can not only use the same characters as you would in a String, but also interpolate Symbols as well. Syntactically, a Symbol needs a colon : prepended. Symbols are actually so similar to Strings that converting between the two is very simple and consistent.&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello&amp;quot;&lt;br /&gt;
  :hello&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  bang = &amp;quot;!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; &amp;quot;hello world!&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; :&amp;quot;hello world!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash] is a collection of Key-Value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.&lt;br /&gt;
&lt;br /&gt;
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. There are three different ways to create a Hashes. &lt;br /&gt;
&lt;br /&gt;
1. Using the keyword  '''new'''.&lt;br /&gt;
  &lt;br /&gt;
  z = Hash.new&lt;br /&gt;
  z[&amp;quot;a&amp;quot;] = 'Hello'&lt;br /&gt;
  z[&amp;quot;b&amp;quot;] = 'Hi'&lt;br /&gt;
  z[&amp;quot;c&amp;quot;] = 'Hey'&lt;br /&gt;
  &lt;br /&gt;
  puts z['a']&lt;br /&gt;
  &amp;gt;&amp;gt;Hello&lt;br /&gt;
  &lt;br /&gt;
  puts z['b']&lt;br /&gt;
  &amp;gt;&amp;gt;Hi&lt;br /&gt;
  &lt;br /&gt;
  puts z['c']&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
2. Using special syntax  '''Hash[ ]'''. &lt;br /&gt;
  &lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Block] is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
This example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you don't have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambdas in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterator] is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closure] in Ruby is a block that closes over. Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x). More about currying may be learnt from [http://en.wikipedia.org/wiki/Currying here]. Currying is a very interesting feature and together with closures, it provides a flexibility to ruby at ease which is not easily achievable in java or c++.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language that provides easy programming. The features of Ruby have made it a very popular and extensible language.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/ &amp;quot;Programming Ruby - The Pragmatic Programmer's Guide&amp;quot;]&amp;lt;br&amp;gt; &lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Object Oriented Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-lang.org/en/about/ Features of Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closures]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterators in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules and Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Duck_typing Duck Typing - Unbounded Polymorphism]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html Regular expressions]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interactive_Ruby_Shell Interactive Ruby Browser]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Array.html Array class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/strings.html String]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Blocks, Procs and Lambdas]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.csc.ncsu.edu/faculty/efg/517/f11/schedule CSC517 Class notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51955</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51955"/>
		<updated>2011-10-15T00:22:16Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamic], [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 reflective], [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented], [http://en.wikipedia.org/wiki/Interpreted_language single-pass interpreted] programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports [http://en.wikipedia.org/wiki/Functional_programming functional], [http://en.wikipedia.org/wiki/Imperative_programming imperative], reflective, object oriented and many other programming paradigms. Since Ruby was designed to incorporate the best features of scripting and Object oriented languages, Ruby emerged to be a very powerful language with native support for many features that are not in the 'nature' of other languages like Java and C++. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro &amp;quot;Matz&amp;quot; Matsumoto]. The intent of developing Ruby was to have a new language that balances functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than [http://www.perl.org/ Perl] and more Object Oriented than [http://www.python.org/ Python].&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of [http://en.wikipedia.org/wiki/Ruby_on_rails Ruby on Rails] in 2005, an [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, [http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html TIOBE programming language popularity index] state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. [http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Purely object oriented]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. [http://c2.com/cgi/wiki?DynamicTyping Dynamically typed]'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Blocks and Closures]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. [http://www.rubyist.net/~slagell/ruby/iterators.html Built in Iterators]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Multiple Inheritance through Mixins]'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. [http://en.wikipedia.org/wiki/Duck_typing Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;]'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. [http://www.regular-expressions.info/ruby.html Syntax level Perl compatible for regular expressions]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. [http://designpatternsinruby.com/ Built-in support for certain Design Patterns]'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. [http://corelib.rubyonrails.org/classes/GC.html Mark-and-Sweep garbage collection]'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. [http://www.ruby-doc.org/core-1.9.2/Thread.html OS independent threading]'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. [http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Easy interface to C modules]'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. [http://www.ruby-lang.org/en/about/ Portable easily]'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
Here is a little table that highlights the key differences between java and ruby. Many of these exact difference maybe shared by Ruby and other languages like C,C++ etc.&amp;lt;br&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing. &lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Methods are passed to the objects&lt;br /&gt;
| Objects are passed to the methods&lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the [http://en.wikipedia.org/wiki/Interactive_Ruby_Shell IRB]. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by [http://www.eclipse.org/ Eclipse]. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
[http://www.jetbrains.com/ruby/ RubyMine] is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
The ''.rb'' extension is used to save Ruby files. The Ruby programs can be run by the command ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file. The path to the ruby command is specified here.&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
===Functions===&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. Ruby does not use { } parenthesis to specify scope. Instead, the keyword  '''end''' is equivalent to the closing parenthesis.&lt;br /&gt;
&lt;br /&gt;
  def fivetimes(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  fivetimes&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized in the function definition itself. The arguments can be specified without ( ) parentheses. This means parentheses in a function call are optional. We can write  ''fivetimes ( 4 )''  or  ''fivetimes 4'' . &lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greeting(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greeting('Sam', 'Peter', 'Derek')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Sam, Peter, Derek&lt;br /&gt;
&lt;br /&gt;
The user need not specify any return value or have an explicit ''return'' for a function.&lt;br /&gt;
The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object.[http://rubylearning.com/satishtalim/writing_own_ruby_methods.html This] is really helpful to learn more about methods in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
One of the reasons that makes Ruby easier to program is the fact that the programmer need not specify the type for any variable. Variables in Ruby are not given a type. Their “type” depends upon the value assigned to them. This feature of Ruby is called [http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing].&lt;br /&gt;
&lt;br /&gt;
In the examples below, the variables ''PI'', ''a'', ''b'', ''c'' are not given a type by the programmer. At run time, ''PI'' is assigned type ''float'', ''a'' and ''b'' are assigned type ''int'', and ''c'' is assigned type ''array''. &lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  ''#''  and run to the end of the line. &lt;br /&gt;
&lt;br /&gt;
Dynamic typing makes many features like unbounded polymorphism, blocks and closures possible. These features are not available in statically typed languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers, strings and arrays are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;changetoupcase&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;CHANGETOUPCASE&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
  &lt;br /&gt;
  [1,3,6,8].class&lt;br /&gt;
  &amp;gt;&amp;gt;Array&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An [http://www.ruby-doc.org/core-1.9.2/Array.html array] in Ruby is an ordered list of elements. An array can be considered an object of objects. In C, C++, Java, C# and other languages, an array consists of elements of a similar type. In Ruby, there is no such constraint. An array can consist of any object. We can have a single array that has Fixnum, String, Array etc as elements. &lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
  d = ['I am string', 4.5, 1000000, [1,2,3,4]] # elements of different type&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array is illustrated below:&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
  &lt;br /&gt;
  b &amp;lt;&amp;lt; [10,100]&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4, [10, 100]]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  ''b[‐1]''  refers to the last element of  ''b''. &lt;br /&gt;
For example, consider the array ''b'' above.&lt;br /&gt;
&lt;br /&gt;
  puts b[-1]&lt;br /&gt;
  &amp;gt;&amp;gt;[10, 100]&lt;br /&gt;
  &lt;br /&gt;
  puts b[-2]&lt;br /&gt;
  &amp;gt;&amp;gt;4&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][0]&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][1]&lt;br /&gt;
  &amp;gt;&amp;gt;100 &lt;br /&gt;
&lt;br /&gt;
Arrays can also be created using the  ''new''  keyword. ''new''  is used to create objects of any type. &lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Strings==&lt;br /&gt;
&lt;br /&gt;
A [http://www.rubyist.net/~slagell/ruby/strings.html String] in Ruby may be double-quoted (&amp;quot;...&amp;quot;) or single-quoted ('...'). &lt;br /&gt;
&lt;br /&gt;
However, double-quoting and single-quoting have different effects in some cases. A double-quoted string allows character escapes by a leading backslash, and the evaluation of embedded expressions using #{}. A single-quoted string does not interpret this. This is shown in the example below:&lt;br /&gt;
&lt;br /&gt;
  string1 = &amp;quot;abc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;abc&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  string2 = 'efg'&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;a\nb\nc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;a&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
  &amp;gt;&amp;gt;c&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts 'a\nb\n'&lt;br /&gt;
  &amp;gt;&amp;gt;a\nb\nc&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;abcd #{5*3} efg&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;abcd 15 efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts var = &amp;quot; abc &amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot; abc &amp;quot;&lt;br /&gt;
  puts &amp;quot;1234#{var}5678&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;1234 abc 5678&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby strings can be concatenated using + operator. A string can be repeated multiple times using * operator. The example below shows the use of + and * operators. Ruby string handling is more powerful than C. Since Ruby, provides support for regular expressions, string handling and pattern matching can be made very efficiently and easily. &lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; + &amp;quot;bar&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foobar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; * 2&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foofoo&amp;quot;&lt;br /&gt;
&lt;br /&gt;
===Symbols===&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols] in Ruby are Strings, just with an important difference, Symbols are immutable while Strings are not. Mutable objects can be changed after assignment while immutable objects can only be overwritten. Ruby is quite unique in offering mutable Strings, which adds greatly to its expressiveness. However mutable Strings can have their share of issues in terms of creating unexpected results and reduced performance. It is for this reason Ruby also offers programmers the choice of Symbols.&lt;br /&gt;
&lt;br /&gt;
Symbols need not contain only alphanumeric characters, by using quotes you can not only use the same characters as you would in a String, but also interpolate Symbols as well. Syntactically, a Symbol needs a colon : prepended. Symbols are actually so similar to Strings that converting between the two is very simple and consistent.&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello&amp;quot;&lt;br /&gt;
  :hello&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  bang = &amp;quot;!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; &amp;quot;hello world!&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; :&amp;quot;hello world!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash] is a collection of Key-Value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.&lt;br /&gt;
&lt;br /&gt;
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. There are three different ways to create a Hashes. &lt;br /&gt;
&lt;br /&gt;
1. Using the keyword  '''new'''.&lt;br /&gt;
  &lt;br /&gt;
  z = Hash.new&lt;br /&gt;
  z[&amp;quot;a&amp;quot;] = 'Hello'&lt;br /&gt;
  z[&amp;quot;b&amp;quot;] = 'Hi'&lt;br /&gt;
  z[&amp;quot;c&amp;quot;] = 'Hey'&lt;br /&gt;
  &lt;br /&gt;
  puts z['a']&lt;br /&gt;
  &amp;gt;&amp;gt;Hello&lt;br /&gt;
  &lt;br /&gt;
  puts z['b']&lt;br /&gt;
  &amp;gt;&amp;gt;Hi&lt;br /&gt;
  &lt;br /&gt;
  puts z['c']&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
2. Using special syntax  '''Hash[ ]'''. &lt;br /&gt;
  &lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Block] is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
This example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you don't have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambdas in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterator] is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closure] in Ruby is a block that closes over. Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x). More about currying may be learnt from [http://en.wikipedia.org/wiki/Currying here]. Currying is a very interesting feature and together with closures, it provides a flexibility to ruby at ease which is not easily achievable in java or c++.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language that provides easy programming. The features of Ruby have made it a very popular and extensible language.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/ &amp;quot;Programming Ruby - The Pragmatic Programmer's Guide&amp;quot;]&amp;lt;br&amp;gt; &lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Object Oriented Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-lang.org/en/about/ Features of Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closures]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterators in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules and Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Duck_typing Duck Typing - Unbounded Polymorphism]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html Regular expressions]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interactive_Ruby_Shell Interactive Ruby Browser]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Array.html Array class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/strings.html String]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Blocks, Procs and Lambdas]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.csc.ncsu.edu/faculty/efg/517/f11/schedule CSC517 Class notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51954</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51954"/>
		<updated>2011-10-15T00:21:29Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamic], [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 reflective], [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented], [http://en.wikipedia.org/wiki/Interpreted_language single-pass interpreted] programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports [http://en.wikipedia.org/wiki/Functional_programming functional], [http://en.wikipedia.org/wiki/Imperative_programming imperative], reflective, object oriented and many other programming paradigms. Since Ruby was designed to incorporate the best features of scripting and Object oriented languages, Ruby emerged to be a very powerful language with native support for many features that are not in the 'nature' of other languages like Java and C++. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro &amp;quot;Matz&amp;quot; Matsumoto]. The intent of developing Ruby was to have a new language that balances functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than [http://www.perl.org/ Perl] and more Object Oriented than [http://www.python.org/ Python].&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of [http://en.wikipedia.org/wiki/Ruby_on_rails Ruby on Rails] in 2005, an [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, [http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html TIOBE programming language popularity index] state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. [http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Purely object oriented]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. [http://c2.com/cgi/wiki?DynamicTyping Dynamically typed]'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Blocks and Closures]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. [http://www.rubyist.net/~slagell/ruby/iterators.html Built in Iterators]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Multiple Inheritance through Mixins]'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. [http://en.wikipedia.org/wiki/Duck_typing Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;]'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. [http://www.regular-expressions.info/ruby.html Syntax level Perl compatible for regular expressions]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. [http://designpatternsinruby.com/ Built-in support for certain Design Patterns]'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. [http://corelib.rubyonrails.org/classes/GC.html Mark-and-Sweep garbage collection]'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. [http://www.ruby-doc.org/core-1.9.2/Thread.html OS independent threading]'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. [http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Easy interface to C modules]'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. [http://www.ruby-lang.org/en/about/ Portable easily]'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
Here is a little table that highlights the key differences between java and ruby. Many of these exact difference maybe shared by Ruby and other languages like C,C++ etc.&amp;lt;br&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing. &lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Methods are passed to the objects&lt;br /&gt;
| Objects are passed to the methods&lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the [http://en.wikipedia.org/wiki/Interactive_Ruby_Shell IRB]. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by [http://www.eclipse.org/ Eclipse]. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
[http://www.jetbrains.com/ruby/ RubyMine] is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
The ''.rb'' extension is used to save Ruby files. The Ruby programs can be run by the command ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file. The path to the ruby command is specified here.&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
===Functions===&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. Ruby does not use { } parenthesis to specify scope. Instead, the keyword  '''end''' is equivalent to the closing parenthesis.&lt;br /&gt;
&lt;br /&gt;
  def fivetimes(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  fivetimes&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized in the function definition itself. The arguments can be specified without ( ) parentheses. This means parentheses in a function call are optional. We can write  ''fivetimes ( 4 )''  or  ''fivetimes 4'' . &lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greeting(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greeting('Sam', 'Peter', 'Derek')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Sam, Peter, Derek&lt;br /&gt;
&lt;br /&gt;
The user need not specify any return value or have an explicit ''return'' for a function.&lt;br /&gt;
The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object.[http://rubylearning.com/satishtalim/writing_own_ruby_methods.html,'This'] is really helpful to learn more about methods in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
One of the reasons that makes Ruby easier to program is the fact that the programmer need not specify the type for any variable. Variables in Ruby are not given a type. Their “type” depends upon the value assigned to them. This feature of Ruby is called [http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing].&lt;br /&gt;
&lt;br /&gt;
In the examples below, the variables ''PI'', ''a'', ''b'', ''c'' are not given a type by the programmer. At run time, ''PI'' is assigned type ''float'', ''a'' and ''b'' are assigned type ''int'', and ''c'' is assigned type ''array''. &lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  ''#''  and run to the end of the line. &lt;br /&gt;
&lt;br /&gt;
Dynamic typing makes many features like unbounded polymorphism, blocks and closures possible. These features are not available in statically typed languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers, strings and arrays are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;changetoupcase&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;CHANGETOUPCASE&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
  &lt;br /&gt;
  [1,3,6,8].class&lt;br /&gt;
  &amp;gt;&amp;gt;Array&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An [http://www.ruby-doc.org/core-1.9.2/Array.html array] in Ruby is an ordered list of elements. An array can be considered an object of objects. In C, C++, Java, C# and other languages, an array consists of elements of a similar type. In Ruby, there is no such constraint. An array can consist of any object. We can have a single array that has Fixnum, String, Array etc as elements. &lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
  d = ['I am string', 4.5, 1000000, [1,2,3,4]] # elements of different type&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array is illustrated below:&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
  &lt;br /&gt;
  b &amp;lt;&amp;lt; [10,100]&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4, [10, 100]]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  ''b[‐1]''  refers to the last element of  ''b''. &lt;br /&gt;
For example, consider the array ''b'' above.&lt;br /&gt;
&lt;br /&gt;
  puts b[-1]&lt;br /&gt;
  &amp;gt;&amp;gt;[10, 100]&lt;br /&gt;
  &lt;br /&gt;
  puts b[-2]&lt;br /&gt;
  &amp;gt;&amp;gt;4&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][0]&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][1]&lt;br /&gt;
  &amp;gt;&amp;gt;100 &lt;br /&gt;
&lt;br /&gt;
Arrays can also be created using the  ''new''  keyword. ''new''  is used to create objects of any type. &lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Strings==&lt;br /&gt;
&lt;br /&gt;
A [http://www.rubyist.net/~slagell/ruby/strings.html String] in Ruby may be double-quoted (&amp;quot;...&amp;quot;) or single-quoted ('...'). &lt;br /&gt;
&lt;br /&gt;
However, double-quoting and single-quoting have different effects in some cases. A double-quoted string allows character escapes by a leading backslash, and the evaluation of embedded expressions using #{}. A single-quoted string does not interpret this. This is shown in the example below:&lt;br /&gt;
&lt;br /&gt;
  string1 = &amp;quot;abc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;abc&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  string2 = 'efg'&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;a\nb\nc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;a&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
  &amp;gt;&amp;gt;c&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts 'a\nb\n'&lt;br /&gt;
  &amp;gt;&amp;gt;a\nb\nc&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;abcd #{5*3} efg&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;abcd 15 efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts var = &amp;quot; abc &amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot; abc &amp;quot;&lt;br /&gt;
  puts &amp;quot;1234#{var}5678&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;1234 abc 5678&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby strings can be concatenated using + operator. A string can be repeated multiple times using * operator. The example below shows the use of + and * operators. Ruby string handling is more powerful than C. Since Ruby, provides support for regular expressions, string handling and pattern matching can be made very efficiently and easily. &lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; + &amp;quot;bar&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foobar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; * 2&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foofoo&amp;quot;&lt;br /&gt;
&lt;br /&gt;
===Symbols===&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols] in Ruby are Strings, just with an important difference, Symbols are immutable while Strings are not. Mutable objects can be changed after assignment while immutable objects can only be overwritten. Ruby is quite unique in offering mutable Strings, which adds greatly to its expressiveness. However mutable Strings can have their share of issues in terms of creating unexpected results and reduced performance. It is for this reason Ruby also offers programmers the choice of Symbols.&lt;br /&gt;
&lt;br /&gt;
Symbols need not contain only alphanumeric characters, by using quotes you can not only use the same characters as you would in a String, but also interpolate Symbols as well. Syntactically, a Symbol needs a colon : prepended. Symbols are actually so similar to Strings that converting between the two is very simple and consistent.&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello&amp;quot;&lt;br /&gt;
  :hello&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  bang = &amp;quot;!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; &amp;quot;hello world!&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; :&amp;quot;hello world!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash] is a collection of Key-Value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.&lt;br /&gt;
&lt;br /&gt;
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. There are three different ways to create a Hashes. &lt;br /&gt;
&lt;br /&gt;
1. Using the keyword  '''new'''.&lt;br /&gt;
  &lt;br /&gt;
  z = Hash.new&lt;br /&gt;
  z[&amp;quot;a&amp;quot;] = 'Hello'&lt;br /&gt;
  z[&amp;quot;b&amp;quot;] = 'Hi'&lt;br /&gt;
  z[&amp;quot;c&amp;quot;] = 'Hey'&lt;br /&gt;
  &lt;br /&gt;
  puts z['a']&lt;br /&gt;
  &amp;gt;&amp;gt;Hello&lt;br /&gt;
  &lt;br /&gt;
  puts z['b']&lt;br /&gt;
  &amp;gt;&amp;gt;Hi&lt;br /&gt;
  &lt;br /&gt;
  puts z['c']&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
2. Using special syntax  '''Hash[ ]'''. &lt;br /&gt;
  &lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Block] is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
This example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you don't have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambdas in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterator] is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closure] in Ruby is a block that closes over. Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x). More about currying may be learnt from [http://en.wikipedia.org/wiki/Currying here]. Currying is a very interesting feature and together with closures, it provides a flexibility to ruby at ease which is not easily achievable in java or c++.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language that provides easy programming. The features of Ruby have made it a very popular and extensible language.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/ &amp;quot;Programming Ruby - The Pragmatic Programmer's Guide&amp;quot;]&amp;lt;br&amp;gt; &lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Object Oriented Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-lang.org/en/about/ Features of Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closures]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterators in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules and Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Duck_typing Duck Typing - Unbounded Polymorphism]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html Regular expressions]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interactive_Ruby_Shell Interactive Ruby Browser]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Array.html Array class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/strings.html String]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Blocks, Procs and Lambdas]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.csc.ncsu.edu/faculty/efg/517/f11/schedule CSC517 Class notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51953</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51953"/>
		<updated>2011-10-15T00:21:05Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamic], [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 reflective], [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented], [http://en.wikipedia.org/wiki/Interpreted_language single-pass interpreted] programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports [http://en.wikipedia.org/wiki/Functional_programming functional], [http://en.wikipedia.org/wiki/Imperative_programming imperative], reflective, object oriented and many other programming paradigms. Since Ruby was designed to incorporate the best features of scripting and Object oriented languages, Ruby emerged to be a very powerful language with native support for many features that are not in the 'nature' of other languages like Java and C++. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro &amp;quot;Matz&amp;quot; Matsumoto]. The intent of developing Ruby was to have a new language that balances functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than [http://www.perl.org/ Perl] and more Object Oriented than [http://www.python.org/ Python].&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of [http://en.wikipedia.org/wiki/Ruby_on_rails Ruby on Rails] in 2005, an [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, [http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html TIOBE programming language popularity index] state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. [http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Purely object oriented]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. [http://c2.com/cgi/wiki?DynamicTyping Dynamically typed]'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Blocks and Closures]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. [http://www.rubyist.net/~slagell/ruby/iterators.html Built in Iterators]'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Multiple Inheritance through Mixins]'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. [http://en.wikipedia.org/wiki/Duck_typing Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;]'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. [http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. [http://www.regular-expressions.info/ruby.html Syntax level Perl compatible for regular expressions]'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. [http://designpatternsinruby.com/ Built-in support for certain Design Patterns]'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. [http://corelib.rubyonrails.org/classes/GC.html Mark-and-Sweep garbage collection]'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. [http://www.ruby-doc.org/core-1.9.2/Thread.html OS independent threading]'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. [http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Easy interface to C modules]'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. [http://www.ruby-lang.org/en/about/ Portable easily]'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
Here is a little table that highlights the key differences between java and ruby. Many of these exact difference maybe shared by Ruby and other languages like C,C++ etc.&amp;lt;br&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing. &lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Methods are passed to the objects&lt;br /&gt;
| Objects are passed to the methods&lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the [http://en.wikipedia.org/wiki/Interactive_Ruby_Shell IRB]. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by [http://www.eclipse.org/ Eclipse]. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
[http://www.jetbrains.com/ruby/ RubyMine] is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
The ''.rb'' extension is used to save Ruby files. The Ruby programs can be run by the command ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file. The path to the ruby command is specified here.&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
===Functions===&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. Ruby does not use { } parenthesis to specify scope. Instead, the keyword  '''end''' is equivalent to the closing parenthesis.&lt;br /&gt;
&lt;br /&gt;
  def fivetimes(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  fivetimes&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized in the function definition itself. The arguments can be specified without ( ) parentheses. This means parentheses in a function call are optional. We can write  ''fivetimes ( 4 )''  or  ''fivetimes 4'' . &lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greeting(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greeting('Sam', 'Peter', 'Derek')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Sam, Peter, Derek&lt;br /&gt;
&lt;br /&gt;
The user need not specify any return value or have an explicit ''return'' for a function.&lt;br /&gt;
The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object.[http://rubylearning.com/satishtalim/writing_own_ruby_methods.html,This] is really helpful to learn more about methods in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
One of the reasons that makes Ruby easier to program is the fact that the programmer need not specify the type for any variable. Variables in Ruby are not given a type. Their “type” depends upon the value assigned to them. This feature of Ruby is called [http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing].&lt;br /&gt;
&lt;br /&gt;
In the examples below, the variables ''PI'', ''a'', ''b'', ''c'' are not given a type by the programmer. At run time, ''PI'' is assigned type ''float'', ''a'' and ''b'' are assigned type ''int'', and ''c'' is assigned type ''array''. &lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  ''#''  and run to the end of the line. &lt;br /&gt;
&lt;br /&gt;
Dynamic typing makes many features like unbounded polymorphism, blocks and closures possible. These features are not available in statically typed languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers, strings and arrays are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;changetoupcase&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;CHANGETOUPCASE&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
  &lt;br /&gt;
  [1,3,6,8].class&lt;br /&gt;
  &amp;gt;&amp;gt;Array&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An [http://www.ruby-doc.org/core-1.9.2/Array.html array] in Ruby is an ordered list of elements. An array can be considered an object of objects. In C, C++, Java, C# and other languages, an array consists of elements of a similar type. In Ruby, there is no such constraint. An array can consist of any object. We can have a single array that has Fixnum, String, Array etc as elements. &lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
  d = ['I am string', 4.5, 1000000, [1,2,3,4]] # elements of different type&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array is illustrated below:&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
  &lt;br /&gt;
  b &amp;lt;&amp;lt; [10,100]&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4, [10, 100]]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  ''b[‐1]''  refers to the last element of  ''b''. &lt;br /&gt;
For example, consider the array ''b'' above.&lt;br /&gt;
&lt;br /&gt;
  puts b[-1]&lt;br /&gt;
  &amp;gt;&amp;gt;[10, 100]&lt;br /&gt;
  &lt;br /&gt;
  puts b[-2]&lt;br /&gt;
  &amp;gt;&amp;gt;4&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][0]&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][1]&lt;br /&gt;
  &amp;gt;&amp;gt;100 &lt;br /&gt;
&lt;br /&gt;
Arrays can also be created using the  ''new''  keyword. ''new''  is used to create objects of any type. &lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Strings==&lt;br /&gt;
&lt;br /&gt;
A [http://www.rubyist.net/~slagell/ruby/strings.html String] in Ruby may be double-quoted (&amp;quot;...&amp;quot;) or single-quoted ('...'). &lt;br /&gt;
&lt;br /&gt;
However, double-quoting and single-quoting have different effects in some cases. A double-quoted string allows character escapes by a leading backslash, and the evaluation of embedded expressions using #{}. A single-quoted string does not interpret this. This is shown in the example below:&lt;br /&gt;
&lt;br /&gt;
  string1 = &amp;quot;abc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;abc&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  string2 = 'efg'&lt;br /&gt;
  &amp;gt;&amp;gt; &amp;quot;efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;a\nb\nc&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;a&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
  &amp;gt;&amp;gt;c&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts 'a\nb\n'&lt;br /&gt;
  &amp;gt;&amp;gt;a\nb\nc&lt;br /&gt;
  &amp;gt;&amp;gt;nil&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;abcd #{5*3} efg&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;abcd 15 efg&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts var = &amp;quot; abc &amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot; abc &amp;quot;&lt;br /&gt;
  puts &amp;quot;1234#{var}5678&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;1234 abc 5678&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby strings can be concatenated using + operator. A string can be repeated multiple times using * operator. The example below shows the use of + and * operators. Ruby string handling is more powerful than C. Since Ruby, provides support for regular expressions, string handling and pattern matching can be made very efficiently and easily. &lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; + &amp;quot;bar&amp;quot;&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foobar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  puts &amp;quot;foo&amp;quot; * 2&lt;br /&gt;
  &amp;gt;&amp;gt;&amp;quot;foofoo&amp;quot;&lt;br /&gt;
&lt;br /&gt;
===Symbols===&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols] in Ruby are Strings, just with an important difference, Symbols are immutable while Strings are not. Mutable objects can be changed after assignment while immutable objects can only be overwritten. Ruby is quite unique in offering mutable Strings, which adds greatly to its expressiveness. However mutable Strings can have their share of issues in terms of creating unexpected results and reduced performance. It is for this reason Ruby also offers programmers the choice of Symbols.&lt;br /&gt;
&lt;br /&gt;
Symbols need not contain only alphanumeric characters, by using quotes you can not only use the same characters as you would in a String, but also interpolate Symbols as well. Syntactically, a Symbol needs a colon : prepended. Symbols are actually so similar to Strings that converting between the two is very simple and consistent.&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello&amp;quot;&lt;br /&gt;
  :hello&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  bang = &amp;quot;!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; &amp;quot;hello world!&amp;quot;&lt;br /&gt;
  :&amp;quot;hello world#{bang}&amp;quot; # =&amp;gt; :&amp;quot;hello world!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash] is a collection of Key-Value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.&lt;br /&gt;
&lt;br /&gt;
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. There are three different ways to create a Hashes. &lt;br /&gt;
&lt;br /&gt;
1. Using the keyword  '''new'''.&lt;br /&gt;
  &lt;br /&gt;
  z = Hash.new&lt;br /&gt;
  z[&amp;quot;a&amp;quot;] = 'Hello'&lt;br /&gt;
  z[&amp;quot;b&amp;quot;] = 'Hi'&lt;br /&gt;
  z[&amp;quot;c&amp;quot;] = 'Hey'&lt;br /&gt;
  &lt;br /&gt;
  puts z['a']&lt;br /&gt;
  &amp;gt;&amp;gt;Hello&lt;br /&gt;
  &lt;br /&gt;
  puts z['b']&lt;br /&gt;
  &amp;gt;&amp;gt;Hi&lt;br /&gt;
  &lt;br /&gt;
  puts z['c']&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
2. Using special syntax  '''Hash[ ]'''. &lt;br /&gt;
  &lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Block] is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
This example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you don't have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambdas in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterator] is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closure] in Ruby is a block that closes over. Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x). More about currying may be learnt from [http://en.wikipedia.org/wiki/Currying here]. Currying is a very interesting feature and together with closures, it provides a flexibility to ruby at ease which is not easily achievable in java or c++.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language that provides easy programming. The features of Ruby have made it a very popular and extensible language.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/ &amp;quot;Programming Ruby - The Pragmatic Programmer's Guide&amp;quot;]&amp;lt;br&amp;gt; &lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm Object Oriented Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-lang.org/en/about/ Features of Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?DynamicTyping Dynamic Typing]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closures]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/iterators.html Iterators in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules and Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Duck_typing Duck Typing - Unbounded Polymorphism]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html Regular expressions]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interactive_Ruby_Shell Interactive Ruby Browser]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Array.html Array class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/strings.html String]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash class in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Blocks, Procs and Lambdas]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.csc.ncsu.edu/faculty/efg/517/f11/schedule CSC517 Class notes]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51885</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51885"/>
		<updated>2011-10-07T02:46:22Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Blocks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports functional, imperative, reflective, object oriented and many other programming paradigms. Since Ruby was designed to incorporate the best features of scripting and Object oriented languages, Ruby emerged to be a very powerful language with native support for many features that are not in the 'nature' of other languages like java and c++. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro &amp;quot;Matz&amp;quot; Matsumoto]. The intent of developing Ruby was to have a new language that balances functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of [http://en.wikipedia.org/wiki/Ruby_on_rails Ruby on Rails] in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, [http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html TIOBE programming language popularity index] state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
Here is a little table that highlights the key differences between java and ruby. Many of these exact difference maybe shared by Ruby and other languages like C,C++ etc.&amp;lt;br&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing. &lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Methods are passed to the objects&lt;br /&gt;
| Objects are passed to the methods&lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
The ''.rb'' extension is used to save Ruby files. The Ruby programs can be run by the command ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file. The path to the ruby command is specified here.&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
===Functions===&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. Ruby does not use { } parenthesis to specify scope. Instead, the keyword  '''end''' is equivalent to the closing parenthesis.&lt;br /&gt;
&lt;br /&gt;
  def fivetimes(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  fivetimes&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized in the function definition itself. The arguments can be specified without ( ) parentheses. This means parentheses in a function call are optional. We can write  ''fivetimes ( 4 )''  or  ''fivetimes 4'' . &lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greeting(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greeting('Sam', 'Peter', 'Derek')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Sam, Peter, Derek&lt;br /&gt;
&lt;br /&gt;
The user need not specify any return value or have an explicit ''return'' for a function.&lt;br /&gt;
The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
One of the reasons that makes Ruby easier to program is the fact that the programmer need not specify the type for any variable. Variables in Ruby are not given a type. Their “type” depends upon the value assigned to them. This feature of Ruby is called Dynamic Typing.&lt;br /&gt;
&lt;br /&gt;
In the examples below, the variables ''PI'', ''a'', ''b'', ''c'' are not given a type by the programmer. At run time, ''PI'' is assigned type ''float'', ''a'' and ''b'' are assigned type ''int'', and ''c'' is assigned type ''array''. &lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  ''#''  and run to the end of the line. &lt;br /&gt;
&lt;br /&gt;
Dynamic typing makes many features like unbounded polymorphism, blocks and closures possible. These features are not available in statically typed languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers, strings and arrays are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;changetoupcase&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;CHANGETOUPCASE&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
  &lt;br /&gt;
  [1,3,6,8].class&lt;br /&gt;
  &amp;gt;&amp;gt;Array&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. An array can be considered an object of objects. In C, C++, Java, C# and other languages, an array consists of elements of a similar type. In Ruby, there is no such constraint. An array can consist of any object. We can have a single array that has Fixnum, String, Array etc as elements. &lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
  d = ['I am string', 4.5, 1000000, [1,2,3,4]] # elements of different type&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array is illustrated below:&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
  &lt;br /&gt;
  b &amp;lt;&amp;lt; [10,100]&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4, [10, 100]]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  ''b[‐1]''  refers to the last element of  ''b''. &lt;br /&gt;
For example, consider the array ''b'' above.&lt;br /&gt;
&lt;br /&gt;
  puts b[-1]&lt;br /&gt;
  &amp;gt;&amp;gt;[10, 100]&lt;br /&gt;
  &lt;br /&gt;
  puts b[-2]&lt;br /&gt;
  &amp;gt;&amp;gt;4&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][0]&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][1]&lt;br /&gt;
  &amp;gt;&amp;gt;100 &lt;br /&gt;
&lt;br /&gt;
Arrays can also be created using the  ''new''  keyword. ''new''  is used to create objects of any type. &lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Has is a collection of Key-Value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.&lt;br /&gt;
&lt;br /&gt;
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. There are three different ways to create a Hashes. &lt;br /&gt;
&lt;br /&gt;
1. Using the keyword  '''new'''.&lt;br /&gt;
  &lt;br /&gt;
  z = Hash.new&lt;br /&gt;
  z[&amp;quot;a&amp;quot;] = 'Hello'&lt;br /&gt;
  z[&amp;quot;b&amp;quot;] = 'Hi'&lt;br /&gt;
  z[&amp;quot;c&amp;quot;] = 'Hey'&lt;br /&gt;
  &lt;br /&gt;
  puts z['a']&lt;br /&gt;
  &amp;gt;&amp;gt;Hello&lt;br /&gt;
  &lt;br /&gt;
  puts z['b']&lt;br /&gt;
  &amp;gt;&amp;gt;Hi&lt;br /&gt;
  &lt;br /&gt;
  puts z['c']&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
2. Using special syntax  '''Hash[ ]'''. &lt;br /&gt;
  &lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambdas in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x). More about currying may be learnt from [http://en.wikipedia.org/wiki/Currying here]. Currying is a very interesting feature and together with closures, it provides a flexibility to ruby at ease which is not easily achievable in java or c++.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language that provides easy programming. The features of Ruby have made it a very popular and extensible language.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/ &amp;quot;Programming Ruby - The Pragmatic Programmer's Guide&amp;quot;]&amp;lt;br&amp;gt; &lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
CSC517 Class notes&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51883</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51883"/>
		<updated>2011-10-07T02:42:05Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Currying */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports functional, imperative, reflective, object oriented and many other programming paradigms. Since Ruby was designed to incorporate the best features of scripting and Object oriented languages, Ruby emerged to be a very powerful language with native support for many features that are not in the 'nature' of other languages like java and c++. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro &amp;quot;Matz&amp;quot; Matsumoto]. The intent of developing Ruby was to have a new language that balances functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of [http://en.wikipedia.org/wiki/Ruby_on_rails Ruby on Rails] in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, [http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html TIOBE programming language popularity index] state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
Here is a little table that highlights the key differences between java and ruby. Many of these exact difference maybe shared by Ruby and other languages like C,C++ etc.&amp;lt;br&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing. &lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Methods are passed to the objects&lt;br /&gt;
| Objects are passed to the methods&lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
The ''.rb'' extension is used to save Ruby files. The Ruby programs can be run by the command ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file. The path to the ruby command is specified here.&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
===Functions===&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. Ruby does not use { } parenthesis to specify scope. Instead, the keyword  '''end''' is equivalent to the closing parenthesis.&lt;br /&gt;
&lt;br /&gt;
  def fivetimes(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  fivetimes&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized in the function definition itself. The arguments can be specified without ( ) parentheses. This means parentheses in a function call are optional. We can write  ''fivetimes ( 4 )''  or  ''fivetimes 4'' . &lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greeting(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greeting('Sam', 'Peter', 'Derek')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Sam, Peter, Derek&lt;br /&gt;
&lt;br /&gt;
The user need not specify any return value or have an explicit ''return'' for a function.&lt;br /&gt;
The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
One of the reasons that makes Ruby easier to program is the fact that the programmer need not specify the type for any variable. Variables in Ruby are not given a type. Their “type” depends upon the value assigned to them. This feature of Ruby is called Dynamic Typing.&lt;br /&gt;
&lt;br /&gt;
In the examples below, the variables ''PI'', ''a'', ''b'', ''c'' are not given a type by the programmer. At run time, ''PI'' is assigned type ''float'', ''a'' and ''b'' are assigned type ''int'', and ''c'' is assigned type ''array''. &lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  ''#''  and run to the end of the line. &lt;br /&gt;
&lt;br /&gt;
Dynamic typing makes many features like unbounded polymorphism, blocks and closures possible. These features are not available in statically typed languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers, strings and arrays are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;changetoupcase&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;CHANGETOUPCASE&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
  &lt;br /&gt;
  [1,3,6,8].class&lt;br /&gt;
  &amp;gt;&amp;gt;Array&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. An array can be considered an object of objects. In C, C++, Java, C# and other languages, an array consists of elements of a similar type. In Ruby, there is no such constraint. An array can consist of any object. We can have a single array that has Fixnum, String, Array etc as elements. &lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
  d = ['I am string', 4.5, 1000000, [1,2,3,4]] # elements of different type&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array is illustrated below:&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
  &lt;br /&gt;
  b &amp;lt;&amp;lt; [10,100]&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4, [10, 100]]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  ''b[‐1]''  refers to the last element of  ''b''. &lt;br /&gt;
For example, consider the array ''b'' above.&lt;br /&gt;
&lt;br /&gt;
  puts b[-1]&lt;br /&gt;
  &amp;gt;&amp;gt;[10, 100]&lt;br /&gt;
  &lt;br /&gt;
  puts b[-2]&lt;br /&gt;
  &amp;gt;&amp;gt;4&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][0]&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][1]&lt;br /&gt;
  &amp;gt;&amp;gt;100 &lt;br /&gt;
&lt;br /&gt;
Arrays can also be created using the  ''new''  keyword. ''new''  is used to create objects of any type. &lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Has is a collection of Key-Value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.&lt;br /&gt;
&lt;br /&gt;
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. There are three different ways to create a Hashes. &lt;br /&gt;
&lt;br /&gt;
1. Using the keyword  '''new'''.&lt;br /&gt;
  &lt;br /&gt;
  z = Hash.new&lt;br /&gt;
  z[&amp;quot;a&amp;quot;] = 'Hello'&lt;br /&gt;
  z[&amp;quot;b&amp;quot;] = 'Hi'&lt;br /&gt;
  z[&amp;quot;c&amp;quot;] = 'Hey'&lt;br /&gt;
  &lt;br /&gt;
  puts z['a']&lt;br /&gt;
  &amp;gt;&amp;gt;Hello&lt;br /&gt;
  &lt;br /&gt;
  puts z['b']&lt;br /&gt;
  &amp;gt;&amp;gt;Hi&lt;br /&gt;
  &lt;br /&gt;
  puts z['c']&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
2. Using special syntax  '''Hash[ ]'''. &lt;br /&gt;
  &lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x). More about currying may be learnt from [http://en.wikipedia.org/wiki/Currying here]. Currying is a very interesting feature and together with closures, it provides a flexibility to ruby at ease which is not easily achievable in java or c++.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language that provides easy programming. The features of Ruby have made it a very popular and extensible language.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/ &amp;quot;Programming Ruby - The Pragmatic Programmer's Guide&amp;quot;]&amp;lt;br&amp;gt; &lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
CSC517 Class notes&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51875</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51875"/>
		<updated>2011-10-07T02:35:29Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Introduction to Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports functional, imperative, reflective, object oriented and many other programming paradigms. Since Ruby was designed to incorporate the best features of scripting and Object oriented languages, Ruby emerged to be a very powerful language with native support for many features that are not in the 'nature' of other languages like java and c++. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro &amp;quot;Matz&amp;quot; Matsumoto]. The intent of developing Ruby was to have a new language that balances functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of [http://en.wikipedia.org/wiki/Ruby_on_rails Ruby on Rails] in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, [http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html TIOBE programming language popularity index] state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
Here is a little table that highlights the key differences between java and ruby. Many of these exact difference maybe shared by Ruby and other languages like C,C++ etc.&amp;lt;br&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing. &lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Methods are passed to the objects&lt;br /&gt;
| Objects are passed to the methods&lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
The ''.rb'' extension is used to save Ruby files. The Ruby programs can be run by the command ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file. The path to the ruby command is specified here.&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
===Functions===&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. Ruby does not use { } parenthesis to specify scope. Instead, the keyword  '''end''' is equivalent to the closing parenthesis.&lt;br /&gt;
&lt;br /&gt;
  def fivetimes(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  fivetimes&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized in the function definition itself. The arguments can be specified without ( ) parentheses. This means parentheses in a function call are optional. We can write  ''fivetimes ( 4 )''  or  ''fivetimes 4'' . &lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greeting(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greeting('Sam', 'Peter', 'Derek')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Sam, Peter, Derek&lt;br /&gt;
&lt;br /&gt;
The user need not specify any return value or have an explicit ''return'' for a function.&lt;br /&gt;
The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
One of the reasons that makes Ruby easier to program is the fact that the programmer need not specify the type for any variable. Variables in Ruby are not given a type. Their “type” depends upon the value assigned to them. This feature of Ruby is called Dynamic Typing.&lt;br /&gt;
&lt;br /&gt;
In the examples below, the variables ''PI'', ''a'', ''b'', ''c'' are not given a type by the programmer. At run time, ''PI'' is assigned type ''float'', ''a'' and ''b'' are assigned type ''int'', and ''c'' is assigned type ''array''. &lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  ''#''  and run to the end of the line. &lt;br /&gt;
&lt;br /&gt;
Dynamic typing makes many features like unbounded polymorphism, blocks and closures possible. These features are not available in statically typed languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers, strings and arrays are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;changetoupcase&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;CHANGETOUPCASE&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
  &lt;br /&gt;
  [1,3,6,8].class&lt;br /&gt;
  &amp;gt;&amp;gt;Array&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. An array can be considered an object of objects. In C, C++, Java, C# and other languages, an array consists of elements of a similar type. In Ruby, there is no such constraint. An array can consist of any object. We can have a single array that has Fixnum, String, Array etc as elements. &lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
  d = ['I am string', 4.5, 1000000, [1,2,3,4]] # elements of different type&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array is illustrated below:&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
  &lt;br /&gt;
  b &amp;lt;&amp;lt; [10,100]&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4, [10, 100]]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  ''b[‐1]''  refers to the last element of  ''b''. &lt;br /&gt;
For example, consider the array ''b'' above.&lt;br /&gt;
&lt;br /&gt;
  puts b[-1]&lt;br /&gt;
  &amp;gt;&amp;gt;[10, 100]&lt;br /&gt;
  &lt;br /&gt;
  puts b[-2]&lt;br /&gt;
  &amp;gt;&amp;gt;4&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][0]&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][1]&lt;br /&gt;
  &amp;gt;&amp;gt;100 &lt;br /&gt;
&lt;br /&gt;
Arrays can also be created using the  ''new''  keyword. ''new''  is used to create objects of any type. &lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Has is a collection of Key-Value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.&lt;br /&gt;
&lt;br /&gt;
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. There are three different ways to create a Hashes. &lt;br /&gt;
&lt;br /&gt;
1. Using the keyword  '''new'''.&lt;br /&gt;
  &lt;br /&gt;
  z = Hash.new&lt;br /&gt;
  z[&amp;quot;a&amp;quot;] = 'Hello'&lt;br /&gt;
  z[&amp;quot;b&amp;quot;] = 'Hi'&lt;br /&gt;
  z[&amp;quot;c&amp;quot;] = 'Hey'&lt;br /&gt;
  &lt;br /&gt;
  puts z['a']&lt;br /&gt;
  &amp;gt;&amp;gt;Hello&lt;br /&gt;
  &lt;br /&gt;
  puts z['b']&lt;br /&gt;
  &amp;gt;&amp;gt;Hi&lt;br /&gt;
  &lt;br /&gt;
  puts z['c']&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
2. Using special syntax  '''Hash[ ]'''. &lt;br /&gt;
  &lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language that provides easy programming. The features of Ruby have made it a very popular and extensible language.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/ &amp;quot;Programming Ruby - The Pragmatic Programmer's Guide&amp;quot;]&amp;lt;br&amp;gt; &lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
CSC517 Class notes&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51872</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51872"/>
		<updated>2011-10-07T02:30:30Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Ruby vs Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro &amp;quot;Matz&amp;quot; Matsumoto]. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of [http://en.wikipedia.org/wiki/Ruby_on_rails Ruby on Rails] in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, [http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html TIOBE programming language popularity index] state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
Here is a little table that highlights the key differences between java and ruby. Many of these exact difference maybe shared by Ruby and other languages like C,C++ etc.&amp;lt;br&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing. &lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Methods are passed to the objects&lt;br /&gt;
| Objects are passed to the methods&lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
The ''.rb'' extension is used to save Ruby files. The Ruby programs can be run by the command ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file. The path to the ruby command is specified here.&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
===Functions===&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. Ruby does not use { } parenthesis to specify scope. Instead, the keyword  '''end''' is equivalent to the closing parenthesis.&lt;br /&gt;
&lt;br /&gt;
  def fivetimes(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  fivetimes&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized in the function definition itself. The arguments can be specified without ( ) parentheses. This means parentheses in a function call are optional. We can write  ''fivetimes ( 4 )''  or  ''fivetimes 4'' . &lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greeting(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greeting('Sam', 'Peter', 'Derek')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Sam, Peter, Derek&lt;br /&gt;
&lt;br /&gt;
The user need not specify any return value or have an explicit ''return'' for a function.&lt;br /&gt;
The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
One of the reasons that makes Ruby easier to program is the fact that the programmer need not specify the type for any variable. Variables in Ruby are not given a type. Their “type” depends upon the value assigned to them. This feature of Ruby is called Dynamic Typing.&lt;br /&gt;
&lt;br /&gt;
In the examples below, the variables ''PI'', ''a'', ''b'', ''c'' are not given a type by the programmer. At run time, ''PI'' is assigned type ''float'', ''a'' and ''b'' are assigned type ''int'', and ''c'' is assigned type ''array''. &lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  ''#''  and run to the end of the line. &lt;br /&gt;
&lt;br /&gt;
Dynamic typing makes many features like unbounded polymorphism, blocks and closures possible. These features are not available in statically typed languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers, strings and arrays are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;changetoupcase&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;CHANGETOUPCASE&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
  &lt;br /&gt;
  [1,3,6,8].class&lt;br /&gt;
  &amp;gt;&amp;gt;Array&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. An array can be considered an object of objects. In C, C++, Java, C# and other languages, an array consists of elements of a similar type. In Ruby, there is no such constraint. An array can consist of any object. We can have a single array that has Fixnum, String, Array etc as elements. &lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
  d = ['I am string', 4.5, 1000000, [1,2,3,4]] # elements of different type&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array is illustrated below:&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
  &lt;br /&gt;
  b &amp;lt;&amp;lt; [10,100]&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4, [10, 100]]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  ''b[‐1]''  refers to the last element of  ''b''. &lt;br /&gt;
For example, consider the array ''b'' above.&lt;br /&gt;
&lt;br /&gt;
  puts b[-1]&lt;br /&gt;
  &amp;gt;&amp;gt;[10, 100]&lt;br /&gt;
  &lt;br /&gt;
  puts b[-2]&lt;br /&gt;
  &amp;gt;&amp;gt;4&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][0]&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
  &lt;br /&gt;
  puts b[-1][1]&lt;br /&gt;
  &amp;gt;&amp;gt;100 &lt;br /&gt;
&lt;br /&gt;
Arrays can also be created using the  ''new''  keyword. ''new''  is used to create objects of any type. &lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Has is a collection of Key-Value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.&lt;br /&gt;
&lt;br /&gt;
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. There are three different ways to create a Hashes. &lt;br /&gt;
&lt;br /&gt;
1. Using the keyword  '''new'''.&lt;br /&gt;
  &lt;br /&gt;
  z = Hash.new&lt;br /&gt;
  z[&amp;quot;a&amp;quot;] = 'Hello'&lt;br /&gt;
  z[&amp;quot;b&amp;quot;] = 'Hi'&lt;br /&gt;
  z[&amp;quot;c&amp;quot;] = 'Hey'&lt;br /&gt;
  &lt;br /&gt;
  puts z['a']&lt;br /&gt;
  &amp;gt;&amp;gt;Hello&lt;br /&gt;
  &lt;br /&gt;
  puts z['b']&lt;br /&gt;
  &amp;gt;&amp;gt;Hi&lt;br /&gt;
  &lt;br /&gt;
  puts z['c']&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
2. Using special syntax  '''Hash[ ]'''. &lt;br /&gt;
  &lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language that provides easy programming. The features of Ruby have made it a very popular and extensible language.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/ &amp;quot;Programming Ruby - The Pragmatic Programmer's Guide&amp;quot;]&amp;lt;br&amp;gt; &lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
CSC517 Class notes&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51792</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51792"/>
		<updated>2011-10-06T23:59:49Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* History */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro &amp;quot;Matz&amp;quot; Matsumoto]. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of [http://en.wikipedia.org/wiki/Ruby_on_rails Ruby on Rails] in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, [http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html TIOBE programming language popularity index] state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
Class notes&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51791</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51791"/>
		<updated>2011-10-06T23:57:28Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Introduction to Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp]. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
Class notes&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51789</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51789"/>
		<updated>2011-10-06T23:57:02Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Introduction to Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp_(programming_language)(programming language) Lisp]. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
Class notes&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51788</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51788"/>
		<updated>2011-10-06T23:55:58Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Introduction to Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby Ruby] is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/SmallTalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel(programming language) Eiffel] and [http://en.wikipedia.org/wiki/Lisp(programming language) Lisp]. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
Class notes&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51787</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51787"/>
		<updated>2011-10-06T23:53:32Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* Blocks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=5&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
   =&amp;gt; nil&lt;br /&gt;
   multiply&lt;br /&gt;
   =&amp;gt;5&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }  &lt;br /&gt;
   &amp;gt;&amp;gt;8 &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
  &amp;gt;&amp;gt;512&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
  &amp;gt;&amp;gt;10&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
Class notes&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51785</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51785"/>
		<updated>2011-10-06T23:50:32Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=8&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }   &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby. &lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://tryruby.org/ tryruby]&amp;lt;br&amp;gt;&lt;br /&gt;
Class notes&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51784</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51784"/>
		<updated>2011-10-06T23:50:13Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=8&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }   &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby. &lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&lt;br /&gt;
[http://tryruby.org/ tryruby]&lt;br /&gt;
Class notes&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51783</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51783"/>
		<updated>2011-10-06T23:50:02Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=8&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }   &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby. &lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.skorks.com/ www.skorks.com]&lt;br /&gt;
[http://ruby-doc.org/ ruby-doc]&lt;br /&gt;
[http://tryruby.org/ tryruby]&lt;br /&gt;
Class notes&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51782</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51782"/>
		<updated>2011-10-06T23:49:25Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=8&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }   &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby. &lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://www.skorks.com/]&lt;br /&gt;
[http://ruby-doc.org/]&lt;br /&gt;
[http://tryruby.org/]&lt;br /&gt;
Class notes&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51780</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51780"/>
		<updated>2011-10-06T23:43:31Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=8&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }   &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby. &lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51779</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51779"/>
		<updated>2011-10-06T23:43:09Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=8&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }   &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby. &lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming link Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming link Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html link Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51778</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51778"/>
		<updated>2011-10-06T23:42:48Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=8&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }   &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby. &lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming link Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming link Functional programming]&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html link Learn Ruby]&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51736</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51736"/>
		<updated>2011-10-06T07:55:32Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=8&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }   &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby. &lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2a_av&amp;diff=51735</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2a_av&amp;diff=51735"/>
		<updated>2011-10-06T07:51:56Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Least Surprise&amp;quot; or &amp;quot;Principle of Least Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto. The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=8&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }   &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby. &lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
 1&lt;br /&gt;
 4&lt;br /&gt;
 9&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
 16&lt;br /&gt;
 25&lt;br /&gt;
 36&lt;br /&gt;
 49&lt;br /&gt;
 64&lt;br /&gt;
 81&lt;br /&gt;
 100&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
 4&lt;br /&gt;
 16&lt;br /&gt;
 36&lt;br /&gt;
 64&lt;br /&gt;
 100&lt;br /&gt;
 144&lt;br /&gt;
 196&lt;br /&gt;
 256&lt;br /&gt;
 324&lt;br /&gt;
 400&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
It does something like this.&lt;br /&gt;
&lt;br /&gt;
 f(x,y) ==currying==&amp;gt; f'(2,y).&lt;br /&gt;
&lt;br /&gt;
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is achieved, again by lambda method, just like closures.&lt;br /&gt;
&lt;br /&gt;
 def multiply (x,y)&lt;br /&gt;
 val = x*y&lt;br /&gt;
 val&lt;br /&gt;
 end&lt;br /&gt;
 =&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
 mulby2 = lambda { |x| multiply(x,2) }&lt;br /&gt;
 mulby3 = lambda { |x| multiply(x,3) }&lt;br /&gt;
&lt;br /&gt;
 mulby2.call 2&lt;br /&gt;
 =&amp;gt; 4&lt;br /&gt;
 mulby3.call 2&lt;br /&gt;
 =&amp;gt; 6&lt;br /&gt;
&lt;br /&gt;
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2a_av&amp;diff=51734</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2a_av&amp;diff=51734"/>
		<updated>2011-10-06T07:36:16Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Lease Surprise&amp;quot; or &amp;quot;Principle of Lease Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro Matsumoto (&amp;quot;Matz&amp;quot;). The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=8&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }   &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby. &lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def multByThree ( n )&lt;br /&gt;
 n*6&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Closure is a function with two additional properties&lt;br /&gt;
&lt;br /&gt;
* It stores the value of all the variables that were in its scope at time of creation.&lt;br /&gt;
* It can be passed around as an object.&lt;br /&gt;
&lt;br /&gt;
In layman terms, a closure is a function that returns another function with atleast one less argument.&lt;br /&gt;
&lt;br /&gt;
 def multByN ( n )&lt;br /&gt;
 closure = lambda { |x| n*x }&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 obj = multByN( 4 )&lt;br /&gt;
 obj.call 5&lt;br /&gt;
 =&amp;gt; 20&lt;br /&gt;
&lt;br /&gt;
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The ''lambda'' in the closure definition refers to a lambda function, which has its roots in functional programming.&lt;br /&gt;
It basically is used to create functions dynamically which is exactly the purpose of a closure as well.&lt;br /&gt;
&lt;br /&gt;
==Currying==&lt;br /&gt;
&lt;br /&gt;
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures ''create'' and ''return'' lambda functions; while currying takes a normal function, and makes a lambda function out of it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2a_av&amp;diff=51733</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2a_av&amp;diff=51733"/>
		<updated>2011-10-06T06:42:20Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Lease Surprise&amp;quot; or &amp;quot;Principle of Lease Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro Matsumoto (&amp;quot;Matz&amp;quot;). The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
==Dynamic Typing==&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
==Hashes==&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=8&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }   &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, it is also possible to use blocks with parametrized functions,&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def displayNumber( parameter )&lt;br /&gt;
  yield( parameter )&lt;br /&gt;
  end&lt;br /&gt;
  displayNumber(10){ |num| puts num }&lt;br /&gt;
&lt;br /&gt;
The above can be extended to two or more parameters&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To compare it with other languages, They are similar to the following,&lt;br /&gt;
* Anonymous inner classes in Java&lt;br /&gt;
* Anonymous functions in Perl&lt;br /&gt;
* Lambda's in Python&lt;br /&gt;
* Function objects in C++&lt;br /&gt;
* Fucntion pointers in C&lt;br /&gt;
&lt;br /&gt;
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby. &lt;br /&gt;
&lt;br /&gt;
==Iterators==&lt;br /&gt;
&lt;br /&gt;
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used. &amp;lt;br&amp;gt;&lt;br /&gt;
There are a few iterators in ruby&amp;lt;br&amp;gt;&lt;br /&gt;
* each&lt;br /&gt;
* times&lt;br /&gt;
* upto and step&lt;br /&gt;
* each_index&lt;br /&gt;
&lt;br /&gt;
 [1,2,3,4,5].each{ |i| puts i*i }&lt;br /&gt;
&lt;br /&gt;
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the ''i*i'' is passed to all the objects, thus effectively squaring all of them.&lt;br /&gt;
&lt;br /&gt;
 5.times{ |i| puts i*i }&lt;br /&gt;
&lt;br /&gt;
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a '''n''' number of times.&amp;lt;br&amp;gt;&lt;br /&gt;
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.&lt;br /&gt;
&lt;br /&gt;
 4.upto(10) { |i| puts i*i }&lt;br /&gt;
&lt;br /&gt;
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.&lt;br /&gt;
&lt;br /&gt;
 2.step(20,2) { |i| puts i*i }&lt;br /&gt;
&lt;br /&gt;
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.&lt;br /&gt;
&lt;br /&gt;
==Closures==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2a_av&amp;diff=51732</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2a_av&amp;diff=51732"/>
		<updated>2011-10-06T05:14:15Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Lease Surprise&amp;quot; or &amp;quot;Principle of Lease Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro Matsumoto (&amp;quot;Matz&amp;quot;). The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Basics===&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
===Dynamic Typing===&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
===Hashes===&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   number=8&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   multiply{ |input|   puts input }   &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method ''multiply''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2a_av&amp;diff=51731</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2a_av&amp;diff=51731"/>
		<updated>2011-10-06T05:12:09Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Lease Surprise&amp;quot; or &amp;quot;Principle of Lease Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro Matsumoto (&amp;quot;Matz&amp;quot;). The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Basics===&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
===Dynamic Typing===&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
===Hashes===&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
  Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
  Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
1. def multiply&lt;br /&gt;
   number=8&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2. def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   &lt;br /&gt;
   multiply{ |input|   puts input }   &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
  multiply{ |input| puts input*input*input }&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function.&lt;br /&gt;
   &lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2a_av&amp;diff=51730</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2a_av&amp;diff=51730"/>
		<updated>2011-10-06T05:10:37Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Lease Surprise&amp;quot; or &amp;quot;Principle of Lease Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro Matsumoto (&amp;quot;Matz&amp;quot;). The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Basics===&lt;br /&gt;
&lt;br /&gt;
Ruby code can be directly evaluated in the Interactive Ruby Browser. The results are immediately returned in the IRB. Ruby code is saved in files with the extension ''.rb'' and can be run by invoking the command  ‘'''ruby filename.rb'''’&lt;br /&gt;
&lt;br /&gt;
The files can be made executable on UNIX systems (without typing  '''ruby''') by adding the shebang line as the first line in the file&lt;br /&gt;
  #!/usr/bin/ruby&lt;br /&gt;
&lt;br /&gt;
The “Hello, world” program can be written as&lt;br /&gt;
  puts “Hello, world!”&lt;br /&gt;
  &amp;gt;&amp;gt;Hello, world!&lt;br /&gt;
&lt;br /&gt;
Functions are defined by the keyword  '''def'''. The keyword  '''end''' is equivalent to the closing parenthesis in other languages.&lt;br /&gt;
&lt;br /&gt;
  def ruby(i = 5)&lt;br /&gt;
    i.times do&lt;br /&gt;
      puts &amp;quot;Ruby&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
  &amp;gt;&amp;gt;Ruby&lt;br /&gt;
&lt;br /&gt;
The arguments to a function can be initialized. Parentheses in a function call are optional. So we can write  '''hello ( 4 )'''  or  '''hello 4''' . Both mean the same.&lt;br /&gt;
&lt;br /&gt;
Function arguments can be aggregated –&lt;br /&gt;
  def greet(*names)&lt;br /&gt;
    puts &amp;quot;Hello #{names.join(', ')}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  greet('Amy', 'Beth', 'John')&lt;br /&gt;
  &lt;br /&gt;
  &amp;gt;&amp;gt;Hello Amy, Beth, John&lt;br /&gt;
&lt;br /&gt;
The last statement in the function is the return value. There is no need for an explicit  '''return''' . And all statements return a value.&lt;br /&gt;
&lt;br /&gt;
===Dynamic Typing===&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not given a type. Their “type” depends upon the&lt;br /&gt;
value assigned to them.&lt;br /&gt;
&lt;br /&gt;
  PI = 3.14159 #By convention, constants have uppercase names&lt;br /&gt;
  a = 5&lt;br /&gt;
  b = 23&lt;br /&gt;
  c = [1,1,2,3,5,8] #This is an array declaration&lt;br /&gt;
&lt;br /&gt;
Comments in Ruby start with a  '''#'''  and run to the end of the line. Dynamic typing makes many features possible, such as unbounded&lt;br /&gt;
polymorphism and blocks, which are not available in statically typed&lt;br /&gt;
languages such as Java and C++.&lt;br /&gt;
&lt;br /&gt;
Everything is an object since Ruby is purely object-oriented. Even numbers and strings are objects.&lt;br /&gt;
&lt;br /&gt;
  5.succ&lt;br /&gt;
  &amp;gt;&amp;gt;6&lt;br /&gt;
  &lt;br /&gt;
  &amp;quot;object&amp;quot;.upcase&lt;br /&gt;
  &amp;gt;&amp;gt;OBJECT&lt;br /&gt;
  &lt;br /&gt;
  [3,1,4,2].sort&lt;br /&gt;
  &amp;gt;&amp;gt;[1,2,3,4]&lt;br /&gt;
  &lt;br /&gt;
  “Ruby”.class&lt;br /&gt;
  &amp;gt;&amp;gt;String&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
An array in Ruby is an ordered list of elements. Simple examples illustrate the use of arrays:&lt;br /&gt;
&lt;br /&gt;
  a = []        # empty array&lt;br /&gt;
  b = [1, 2, 3] # array of FixNums&lt;br /&gt;
  c = ['Hello', 'Ruby'] #array of strings&lt;br /&gt;
&lt;br /&gt;
Special syntax to create an array of strings without the quotes:&lt;br /&gt;
  d = %w{Hello Ruby}&lt;br /&gt;
&lt;br /&gt;
Appending elements to an Array –&lt;br /&gt;
  b &amp;lt;&amp;lt; 4&lt;br /&gt;
  &amp;gt;&amp;gt;[1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Array indexes start from zero. Negative indexes count backward. So  '''b[‐1]'''  refers to the last element of  '''b''' .&lt;br /&gt;
Arrays can also be created using the  '''new'''  keyword. '''new'''  is used to create objects of any type. More about  '''new'''  will follow later.&lt;br /&gt;
&lt;br /&gt;
  n = Array.new&lt;br /&gt;
  n &amp;lt;&amp;lt; 10&lt;br /&gt;
  puts n&lt;br /&gt;
  &amp;gt;&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
===Hashes===&lt;br /&gt;
&lt;br /&gt;
Ruby has syntax support for hashes. Wondering what’s a “hash”? It's an “array” that can be indexed by arbitrary keys. Hashes can be created in three ways.&lt;br /&gt;
&lt;br /&gt;
1. Using the keyword  '''new''' ,&lt;br /&gt;
  h = Hash.new&lt;br /&gt;
  h[&amp;quot;a&amp;quot;] = 'b'&lt;br /&gt;
  &lt;br /&gt;
  puts h['a']&lt;br /&gt;
  &amp;gt;&amp;gt;b&lt;br /&gt;
&lt;br /&gt;
2. Using special syntax  '''Hash[ ]'''  or&lt;br /&gt;
  numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4]&lt;br /&gt;
  &lt;br /&gt;
  puts numbers['three']&lt;br /&gt;
  &amp;gt;&amp;gt;3&lt;br /&gt;
&lt;br /&gt;
3. Using  '''{ }'''&lt;br /&gt;
  capitals = {&lt;br /&gt;
  &amp;quot;France&amp;quot; =&amp;gt; &amp;quot;Paris&amp;quot;,&lt;br /&gt;
  &amp;quot;Russia&amp;quot; =&amp;gt; &amp;quot;Moscow&amp;quot;,&lt;br /&gt;
  &amp;quot;Japan&amp;quot; =&amp;gt; &amp;quot;Tokyo&amp;quot;,&lt;br /&gt;
  &amp;quot;India&amp;quot; =&amp;gt; &amp;quot;New Delhi&amp;quot;,&lt;br /&gt;
  &amp;quot;USA&amp;quot; =&amp;gt; &amp;quot;Washington, DC&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  puts capitals[&amp;quot;India&amp;quot;]&lt;br /&gt;
  &amp;gt;&amp;gt;New Delhi&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular. &amp;lt;BR&amp;gt;&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
1. def multiply&lt;br /&gt;
   number=8&lt;br /&gt;
   puts number&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.&amp;lt;br&amp;gt;&lt;br /&gt;
But, blocks offer a alternative to that. ''Yield'' is used to transfer the control from the method to a 'block' and back to the function again.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2. def multiply&lt;br /&gt;
   yield(8)&lt;br /&gt;
   end&lt;br /&gt;
   &lt;br /&gt;
   multiply{ |input|   puts input }   &lt;br /&gt;
&lt;br /&gt;
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.&lt;br /&gt;
Thsis example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you dont have to keep track of the other method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
So, you may choose to make the function do,&lt;br /&gt;
&lt;br /&gt;
3. multiply{ |input| puts input*input*input }&lt;br /&gt;
&lt;br /&gt;
without having to refer back to the original function.&lt;br /&gt;
   &lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2a_av&amp;diff=51690</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2a_av&amp;diff=51690"/>
		<updated>2011-10-03T01:08:23Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction to Ruby=&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. &lt;br /&gt;
&lt;br /&gt;
Ruby is said to incorporate the &amp;quot;Principle of Lease Surprise&amp;quot; or &amp;quot;Principle of Lease Astonishment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Ruby was created in 1993 in Japan, by Yukihiro Matsumoto (&amp;quot;Matz&amp;quot;). The intent of developing Ruby was to have a new language that balance functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.(ref)&lt;br /&gt;
&lt;br /&gt;
===Naming===&lt;br /&gt;
&lt;br /&gt;
The terms &amp;quot;Coral&amp;quot; and &amp;quot;Ruby&amp;quot; were the two proposed names for the language. Matsumoto's choose the term &amp;quot;Ruby&amp;quot; because it was one of his colleague’s birthstone. &lt;br /&gt;
&lt;br /&gt;
===Releases and Versions===&lt;br /&gt;
 &lt;br /&gt;
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.0 was released in 1996.&lt;br /&gt;
&lt;br /&gt;
Ruby 1.3 was the next release in 1999. &lt;br /&gt;
&lt;br /&gt;
Since then several versions of Ruby have been released with added concepts and features. &lt;br /&gt;
&lt;br /&gt;
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series. &lt;br /&gt;
&lt;br /&gt;
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.   &lt;br /&gt;
&lt;br /&gt;
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index (link) state Ruby as the 10th most popular language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
'''1. Purely object oriented'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.&lt;br /&gt;
&lt;br /&gt;
'''2. Dynamically types'''&lt;br /&gt;
&lt;br /&gt;
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby. &lt;br /&gt;
&lt;br /&gt;
'''3. Blocks and Closures'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.&lt;br /&gt;
&lt;br /&gt;
'''4. Built in Iterators'''&lt;br /&gt;
&lt;br /&gt;
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.&lt;br /&gt;
&lt;br /&gt;
'''5. Multiple Inheritance through Mixins'''&lt;br /&gt;
&lt;br /&gt;
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
'''6. Unbounded Polymorphism - &amp;quot;Duck Typing&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it.&lt;br /&gt;
This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work.  &lt;br /&gt;
This is unbounded polymorphism, which can only be found in dynamically typed languages.&lt;br /&gt;
&lt;br /&gt;
'''7. Reflection'''&lt;br /&gt;
&lt;br /&gt;
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection.  For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time. &lt;br /&gt;
&lt;br /&gt;
'''8. Metaprogramming'''&lt;br /&gt;
&lt;br /&gt;
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.&lt;br /&gt;
&lt;br /&gt;
'''9. Syntax level Perl compatible for regular expressions'''&lt;br /&gt;
&lt;br /&gt;
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport. &lt;br /&gt;
&lt;br /&gt;
'''10. Built-in support for certain Design Patterns'''&lt;br /&gt;
&lt;br /&gt;
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems. &lt;br /&gt;
&lt;br /&gt;
'''11. Mark-and-Sweep garbage collection'''&lt;br /&gt;
&lt;br /&gt;
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management. &lt;br /&gt;
&lt;br /&gt;
'''12. OS independent threading'''&lt;br /&gt;
&lt;br /&gt;
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.&lt;br /&gt;
&lt;br /&gt;
'''13. Easy interface to C modules'''&lt;br /&gt;
&lt;br /&gt;
Ruby provides interface to C modules which help programmers integrate C with Ruby.&lt;br /&gt;
&lt;br /&gt;
'''14. Portable easily'''&lt;br /&gt;
&lt;br /&gt;
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.&lt;br /&gt;
&lt;br /&gt;
==Downloads and Installation==&lt;br /&gt;
&lt;br /&gt;
The main website for Ruby is http://www.ruby-lang.org/en/. &lt;br /&gt;
&lt;br /&gt;
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.&lt;br /&gt;
&lt;br /&gt;
One can also use Ruby online at http://www.tryruby.org/.&lt;br /&gt;
&lt;br /&gt;
==Interactive Ruby Browser - IRB==&lt;br /&gt;
&lt;br /&gt;
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB.&lt;br /&gt;
It can be started in two ways:&lt;br /&gt;
&lt;br /&gt;
1. Directly open the IRB by clicking on the Interactive Ruby icon.&lt;br /&gt;
&lt;br /&gt;
2. In the command prompt, type &amp;quot;irb&amp;quot; to start a new IRB session.&lt;br /&gt;
&lt;br /&gt;
===Other Editors for Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.&lt;br /&gt;
&lt;br /&gt;
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby vs Java ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Ruby&lt;br /&gt;
! Java&lt;br /&gt;
|-&lt;br /&gt;
| Interpreted language&lt;br /&gt;
| Compiled language&lt;br /&gt;
|-&lt;br /&gt;
| Dynamic Typing&lt;br /&gt;
| Static typing.&lt;br /&gt;
|-&lt;br /&gt;
| Purely OO. Everything is an object&lt;br /&gt;
| There are primitives and objects.&lt;br /&gt;
|-&lt;br /&gt;
| Unbounded Polymorphism&lt;br /&gt;
| Inheritances and interfaces used.&lt;br /&gt;
|-&lt;br /&gt;
| Native support for Regular Expressions&lt;br /&gt;
| Not native support; java.util.regex provides it.&lt;br /&gt;
|-&lt;br /&gt;
| Nil is an object which says there is no object&lt;br /&gt;
| Null indicates the absence of objects&lt;br /&gt;
|-&lt;br /&gt;
| Methods calls are treated as messages at runtime&lt;br /&gt;
| Method calls are compiled &lt;br /&gt;
|-&lt;br /&gt;
| Native support for hashes&lt;br /&gt;
| Hashes are suported by Collections framework&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51353</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51353"/>
		<updated>2011-09-29T19:29:42Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
This chapter aims to provide the user a nice and smooth transition to programming in Ruby. This article is written in the stand-point of a user who is already familiar with the general Hows and Whats and Whys of programming in C++ and/or Java. This article also provides numerous code snippets that the user can use to get used to Ruby.&lt;br /&gt;
&lt;br /&gt;
==What is Ruby?==&lt;br /&gt;
Ruby is a reflective, dynamically typed, object-oriented and single-pass interpreted language. Lot of Jargon? Hmm...&amp;lt;br&amp;gt;&lt;br /&gt;
'''Reflective - ''' Ruby can modify its behavior at one or more points, based on the input it receives pertaining to that.&amp;lt;br&amp;gt;&lt;br /&gt;
'''Object oriented - ''' Ruby is a purely object oriented language. There are absolutely no primitive data types at all in Ruby. Or in other words, every entity and data type is treated as an object in Ruby.&amp;lt;br&amp;gt;&lt;br /&gt;
'''Dynamically typed - ''' The objects get their data type and behavior only AFTER an assignment has been made into it. In other words, after a variable is ''declared'', it can end up being an object of ''any'' type.&amp;lt;br&amp;gt;&lt;br /&gt;
'''Single pass interpreted - ''' The program is executed line by line after the call is made. This is one of the main reasons for ruby's flexibility.&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
In this section we shall focus on installing Ruby on two different platforms, Windows and Linux.&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
For windows all the user needs to do is visit [http://rubyinstaller.org/downloads/ this page] and download the Ruby installer. It is a self extracting executable, so all the user has to do is click and wait!&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51352</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51352"/>
		<updated>2011-09-29T19:28:19Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
This chapter aims to provide the user a nice and smooth transition to programming in Ruby. This article is written in the stand-point of a user who is already familiar with the general Hows and Whats and Whys of programming in C++ and/or Java. This article also provides numerous code snippets that the user can use to get used to Ruby.&lt;br /&gt;
&lt;br /&gt;
==What is Ruby?==&lt;br /&gt;
Ruby is a reflective, dynamically typed, object-oriented and single-pass interpreted language. Lot of Jargon? Hmm...&lt;br /&gt;
'''Reflective - ''' Ruby can modify its behavior at one or more points, based on the input it receives pertaining to that.&lt;br /&gt;
'''Object oriented - ''' Ruby is a purely object oriented language. There are absolutely no primitive data types at all in Ruby. Or in other words, every entity and data type is treated as an object in Ruby.&lt;br /&gt;
'''Dynamically typed - ''' The objects get their data type and behavior only AFTER an assignment has been made into it. In other words, after a variable is ''declared'', it can end up being an object of ''any'' type.&lt;br /&gt;
'''Single pass interpreted - ''' The program is executed line by line after the call is made. This is one of the main reasons for ruby's flexibility.&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
In this section we shall focus on installing Ruby on two different platforms, Windows and Linux.&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
For windows all the user needs to do is visit [http://rubyinstaller.org/downloads/ this page] and download the Ruby installer. It is a self extracting executable, so all the user has to do is click and wait!&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51351</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_av&amp;diff=51351"/>
		<updated>2011-09-29T19:08:16Z</updated>

		<summary type="html">&lt;p&gt;Vsuresw: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
This chapter aims to provide the user a nice and smooth transition to programming in Ruby. This article is written in the stand-point of a user who is already familiar with the general Hows and Whats and Whys of programming in C++ and/or Java. This article also provides numerous code snippets that the user can use to get used to Ruby.&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
In this section we shall focus on installing Ruby on two different platforms, Windows and Linux.&lt;br /&gt;
&lt;br /&gt;
== Windows == &lt;br /&gt;
[http://rubyinstaller.org/downloads/ Download RubyInstaller here]&lt;/div&gt;</summary>
		<author><name>Vsuresw</name></author>
	</entry>
</feed>