<?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=Arbukkap</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=Arbukkap"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Arbukkap"/>
	<updated>2026-06-09T10:12:59Z</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_4g_as&amp;diff=54459</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=54459"/>
		<updated>2011-10-31T18:48:21Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a [http://en.wikipedia.org/wiki/Compiler compiler]. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for [http://en.wikipedia.org/wiki/Domain-specific_language Domain Specific Languages]. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. [http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails] is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
5)      Lately, meta programming has many advantages in the field of [http://en.wikipedia.org/wiki/Artificial_intelligence Artificial Intelligence].&lt;br /&gt;
&lt;br /&gt;
== Evolution of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of Array (since Array class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    String.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_reader( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
              String.class_eval &amp;quot;def #{instance_variable};@#{instance_variable};end&amp;quot; &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object of class String. So, we use this class to redefine methods and also to add new methods. For instance, we can add new instance methods of a class by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. We can also use techniques like instance_variables to indicate the instance_variables in the class. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name.&lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          4.times do&lt;br /&gt;
     	    class A&lt;br /&gt;
              def method1&lt;br /&gt;
                puts &amp;quot;This is method1&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so the definition of the class A is evaluated four times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class A will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class A&lt;br /&gt;
            def method1&lt;br /&gt;
               puts &amp;quot;This is method1&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def method1&lt;br /&gt;
               puts &amp;quot;This is method1&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def method1&lt;br /&gt;
               puts &amp;quot;This is method1&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
           def method1&lt;br /&gt;
               puts &amp;quot;This is method1&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined four times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class A will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class A&lt;br /&gt;
            def method1&lt;br /&gt;
               puts &amp;quot;This is method1&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;[http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes]&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;[http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods]&amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby.&amp;lt;ref&amp;gt;[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules in Ruby]&amp;lt;/ref&amp;gt; &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby]&amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
== Conclusion == &lt;br /&gt;
So, we can conclude by saying that various techniques and properties like singleton classes, method aliasing, class definitions and modules help in implementing this simple yet powerful feature called meta-programming efficiently in Ruby. Thus we can also say that Ruby has this powerful feature inbuilt within the language and can implement it with all the various techniques as discussed in this page.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 1. Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;br /&gt;
&lt;br /&gt;
[http://assets.en.oreilly.com/1/event/12/Metaprogramming%20in%20Ruby%20Presentation%201.pdf 2. Meta Programming in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://rubysource.com/ruby-metaprogramming-part-i/ 3. Ruby Metaprogramming: Part I]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=54458</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=54458"/>
		<updated>2011-10-31T18:31:31Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a [http://en.wikipedia.org/wiki/Compiler compiler]. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for [http://en.wikipedia.org/wiki/Domain-specific_language Domain Specific Languages]. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. [http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails] is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
5)      Lately, meta programming has many advantages in the field of [http://en.wikipedia.org/wiki/Artificial_intelligence Artificial Intelligence].&lt;br /&gt;
&lt;br /&gt;
== Evolution of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of Array (since Array class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name.&lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          4.times do&lt;br /&gt;
     	    class A&lt;br /&gt;
              def method1&lt;br /&gt;
                puts &amp;quot;This is method1&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so the definition of the class A is evaluated four times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class A will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class A&lt;br /&gt;
            def method1&lt;br /&gt;
               puts &amp;quot;This is method1&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def method1&lt;br /&gt;
               puts &amp;quot;This is method1&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def method1&lt;br /&gt;
               puts &amp;quot;This is method1&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
           def method1&lt;br /&gt;
               puts &amp;quot;This is method1&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined four times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class A will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class A&lt;br /&gt;
            def method1&lt;br /&gt;
               puts &amp;quot;This is method1&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;[http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes]&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;[http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods]&amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby.&amp;lt;ref&amp;gt;[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules in Ruby]&amp;lt;/ref&amp;gt; &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby]&amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
== Conclusion == &lt;br /&gt;
So, we can conclude by saying that various techniques and properties like singleton classes, method aliasing, class definitions and modules help in implementing this simple yet powerful feature called meta-programming efficiently in Ruby. Thus we can also say that Ruby has this powerful feature inbuilt within the language and can implement it with all the various techniques as discussed in this page.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 1. Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;br /&gt;
&lt;br /&gt;
[http://assets.en.oreilly.com/1/event/12/Metaprogramming%20in%20Ruby%20Presentation%201.pdf 2. Meta Programming in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://rubysource.com/ruby-metaprogramming-part-i/ 3. Ruby Metaprogramming: Part I]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53338</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53338"/>
		<updated>2011-10-20T23:10:57Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a [http://en.wikipedia.org/wiki/Compiler compiler]. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for [http://en.wikipedia.org/wiki/Domain-specific_language Domain Specific Languages]. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. [http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails] is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
5)      Lately, meta programming has many advantages in the field of [http://en.wikipedia.org/wiki/Artificial_intelligence Artificial Intelligence].&lt;br /&gt;
&lt;br /&gt;
== Evolution of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;[http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes]&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;[http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods]&amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby.&amp;lt;ref&amp;gt;[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules in Ruby]&amp;lt;/ref&amp;gt; &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby]&amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 1. Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;br /&gt;
&lt;br /&gt;
[http://assets.en.oreilly.com/1/event/12/Metaprogramming%20in%20Ruby%20Presentation%201.pdf 2. Meta Programming in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://rubysource.com/ruby-metaprogramming-part-i/ 3. Ruby Metaprogramming: Part I]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53158</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53158"/>
		<updated>2011-10-20T19:06:46Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: Blanked the page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53157</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53157"/>
		<updated>2011-10-20T19:06:15Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a [http://en.wikipedia.org/wiki/Compiler compiler]. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for [http://en.wikipedia.org/wiki/Domain-specific_language Domain Specific Languages]. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. [http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails] is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
5)      Lately, meta programming has many advantages in the field of [http://en.wikipedia.org/wiki/Artificial_intelligence Artificial Intelligence].&lt;br /&gt;
&lt;br /&gt;
== Evolution of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;[http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes]&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;[http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods]&amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby.&amp;lt;ref&amp;gt;[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules in Ruby]&amp;lt;/ref&amp;gt; &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby]&amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 1. Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;br /&gt;
&lt;br /&gt;
[http://assets.en.oreilly.com/1/event/12/Metaprogramming%20in%20Ruby%20Presentation%201.pdf 2. Meta Programming in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://rubysource.com/ruby-metaprogramming-part-i/ 3. Ruby Metaprogramming: Part I]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53156</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53156"/>
		<updated>2011-10-20T19:05:53Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: Blanked the page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53155</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53155"/>
		<updated>2011-10-20T19:04:49Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a [http://en.wikipedia.org/wiki/Compiler compiler]. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for [http://en.wikipedia.org/wiki/Domain-specific_language Domain Specific Languages]. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. [http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails] is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
5)      Lately, meta programming has many advantages in the field of [http://en.wikipedia.org/wiki/Artificial_intelligence Artificial Intelligence].&lt;br /&gt;
&lt;br /&gt;
== Evolution of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;[http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes]&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;[http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods]&amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby.&amp;lt;ref&amp;gt;[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules in Ruby]&amp;lt;/ref&amp;gt; &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby]&amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 1. Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;br /&gt;
&lt;br /&gt;
[http://assets.en.oreilly.com/1/event/12/Metaprogramming%20in%20Ruby%20Presentation%201.pdf 2. Meta Programming in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://rubysource.com/ruby-metaprogramming-part-i/ 3. Ruby Metaprogramming: Part I]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53154</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53154"/>
		<updated>2011-10-20T19:04:38Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a [http://en.wikipedia.org/wiki/Compiler compiler]. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for [http://en.wikipedia.org/wiki/Domain-specific_language Domain Specific Languages]. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. [http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails] is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
5)      Lately, meta programming has many advantages in the field of [http://en.wikipedia.org/wiki/Artificial_intelligence Artificial Intelligence].&lt;br /&gt;
&lt;br /&gt;
== Evolution of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;[http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes]&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;[http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods]&amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby.&amp;lt;ref&amp;gt;[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules in Ruby]&amp;lt;/ref&amp;gt; &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby]&amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 1. Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;br /&gt;
&lt;br /&gt;
[http://assets.en.oreilly.com/1/event/12/Metaprogramming%20in%20Ruby%20Presentation%201.pdf 2. Meta Programming in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://rubysource.com/ruby-metaprogramming-part-i/ 3.Ruby Metaprogramming: Part I]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53151</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53151"/>
		<updated>2011-10-20T19:03:59Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a [http://en.wikipedia.org/wiki/Compiler compiler]. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for [http://en.wikipedia.org/wiki/Domain-specific_language Domain Specific Languages]. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. [http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails] is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
5)      Lately, meta programming has many advantages in the field of [http://en.wikipedia.org/wiki/Artificial_intelligence Artificial Intelligence].&lt;br /&gt;
&lt;br /&gt;
== Evolution of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;[http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes]&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;[http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods]&amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby.&amp;lt;ref&amp;gt;[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules in Ruby]&amp;lt;/ref&amp;gt; &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby]&amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 1.Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;br /&gt;
&lt;br /&gt;
[http://assets.en.oreilly.com/1/event/12/Metaprogramming%20in%20Ruby%20Presentation%201.pdf 2.Meta Programming in Ruby]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53150</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53150"/>
		<updated>2011-10-20T18:56:05Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a [http://en.wikipedia.org/wiki/Compiler compiler]. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for [http://en.wikipedia.org/wiki/Domain-specific_language Domain Specific Languages]. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. [http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails] is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
5)      Lately, meta programming has many advantages in the field of [http://en.wikipedia.org/wiki/Artificial_intelligence Artificial Intelligence].&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;[http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes]&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;[http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods]&amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby.&amp;lt;ref&amp;gt;[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules in Ruby]&amp;lt;/ref&amp;gt; &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby]&amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 1.Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;br /&gt;
&lt;br /&gt;
[http://assets.en.oreilly.com/1/event/12/Metaprogramming%20in%20Ruby%20Presentation%201.pdf 2.Meta Programming in Ruby]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53149</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53149"/>
		<updated>2011-10-20T18:55:39Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a [http://en.wikipedia.org/wiki/Compiler compiler]. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for [http://en.wikipedia.org/wiki/Domain-specific_language Domain Specific Languages]. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. [http://en.wikipedia.org/wiki/Ruby_(programming_language)#Ruby_on_Rails Ruby on Rails] is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
5)      Lately, meta programming has many advantages in the field of [http://en.wikipedia.org/wiki/Artificial_intelligence Artificial Intelligence].&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;[http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes]&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;[http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods]&amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby.&amp;lt;ref&amp;gt;[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules in Ruby]&amp;lt;/ref&amp;gt; &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby]&amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 1.Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;br /&gt;
&lt;br /&gt;
[http://assets.en.oreilly.com/1/event/12/Metaprogramming%20in%20Ruby%20Presentation%201.pdf 2.Meta Programming in Ruby]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53148</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53148"/>
		<updated>2011-10-20T18:54:33Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a [http://en.wikipedia.org/wiki/Compiler compiler]. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for [http://en.wikipedia.org/wiki/Domain-specific_language Domain Specific Languages]. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
5)      Lately, meta programming has many advantages in the field of [http://en.wikipedia.org/wiki/Artificial_intelligence Artificial Intelligence].&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;[http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes]&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;[http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods]&amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby.&amp;lt;ref&amp;gt;[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules in Ruby]&amp;lt;/ref&amp;gt; &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby]&amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 1.Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;br /&gt;
&lt;br /&gt;
[http://assets.en.oreilly.com/1/event/12/Metaprogramming%20in%20Ruby%20Presentation%201.pdf 2.Meta Programming in Ruby]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53146</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53146"/>
		<updated>2011-10-20T18:53:12Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for [http://en.wikipedia.org/wiki/Domain-specific_language Domain Specific Languages]. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
5)      Lately, meta programming has many advantages in the field of [http://en.wikipedia.org/wiki/Artificial_intelligence Artificial Intelligence].&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;[http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes]&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;[http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods]&amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby.&amp;lt;ref&amp;gt;[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules in Ruby]&amp;lt;/ref&amp;gt; &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby]&amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Futher reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 1.Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;br /&gt;
&lt;br /&gt;
[http://assets.en.oreilly.com/1/event/12/Metaprogramming%20in%20Ruby%20Presentation%201.pdf 2.Meta Programming in Ruby]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53142</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53142"/>
		<updated>2011-10-20T18:48:54Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for [http://en.wikipedia.org/wiki/Domain-specific_language Domain Specific Languages]. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
5)      Lately, meta programming has many advantages in the field of [http://en.wikipedia.org/wiki/Artificial_intelligence Artificial Intelligence].&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods &amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby.&amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules in Ruby&amp;lt;/ref&amp;gt; &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby &amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Futher reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;br /&gt;
&lt;br /&gt;
[http://assets.en.oreilly.com/1/event/12/Metaprogramming%20in%20Ruby%20Presentation%201.pdf Meta Programming in Ruby]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53139</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53139"/>
		<updated>2011-10-20T18:43:48Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods &amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby. &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of multiple inheritance in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby &amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Futher reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;br /&gt;
&lt;br /&gt;
[http://assets.en.oreilly.com/1/event/12/Metaprogramming%20in%20Ruby%20Presentation%201.pdf Meta Programming in Ruby]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53138</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53138"/>
		<updated>2011-10-20T18:43:32Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods &amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby. &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of multiple inheritance in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby &amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Futher reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;br /&gt;
[http://assets.en.oreilly.com/1/event/12/Metaprogramming%20in%20Ruby%20Presentation%201.pdf Meta Programming in Ruby]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53135</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53135"/>
		<updated>2011-10-20T18:41:04Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-module_eval module_eval] method.&amp;lt;ref&amp;gt;http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name &amp;quot;Xyz&amp;quot; in the program’s namespace and make sure it behaves like a normal class as if it is defined using the 'class' keyword.&lt;br /&gt;
&lt;br /&gt;
The class can be instantiated and its attributes can be accessed in the following way.&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-const_get const_get] method and the attribute is set and read using [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_set instance_variable_set] and [http://www.ruby-doc.org/core-1.9.2/Object.html#method-i-instance_variable_get instance_variable_get] methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The [http://www.ruby-doc.org/core-1.9.2/Module.html#method-i-class_eval class_eval] method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods &amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby. &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of multiple inheritance in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby &amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Futher reading ==&lt;br /&gt;
[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Implementation of Meta programming in Ruby and its application on a framework called Rails]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53130</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53130"/>
		<updated>2011-10-20T18:37:28Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and data is nothing but code. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_open_classes.html Open Classes in Ruby]&amp;lt;/ref&amp;gt; helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_overloading_methods.html Method over-loading in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
             if args.size == 2&lt;br /&gt;
                puts 'Two arguments'&lt;br /&gt;
             else&lt;br /&gt;
                puts 'Three arguments'&lt;br /&gt;
             end&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Three arguements &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
    #=&amp;gt; Correct number of arguements passed!!!&lt;br /&gt;
    #=&amp;gt; Two arguements&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton classes] are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&amp;lt;ref&amp;gt;[http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html Meta programming in Ruby Overview]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&amp;lt;ref&amp;gt;[http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html Singleton classes implementation and its uses in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec12.pdf Lecture Notes on Meta programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&amp;lt;ref&amp;gt;[http://www.zenspider.com/Languages/Ruby/QuickRef.html#31 Aliasing representation in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. We can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept.&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing in Ruby]&amp;lt;/ref&amp;gt; Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the module_eval method of class Object.&amp;lt;ref&amp;gt;http://bathroom.baclayon.com/ruby/dynamic.html#class Dynamically creating classes&amp;lt;/ref&amp;gt; Let us see an example of how the above class can be defined using module_eval method.&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method.&amp;lt;ref&amp;gt;http://gregmoreno.ca/how-to-create-a-class-on-the-fly-in-ruby/ How to create a class on the fly in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name “Xyz” in the program’s namespace and make sure it behaves like a normal class as if it is defined using the ‘class’ keyword.&lt;br /&gt;
The class can be instantiated and its attributes can be  accessed in the following way:&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the const_get method of class Object and the attribute is set and read using instance_variable_set and instance_variable_get methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The class_eval method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&amp;lt;ref&amp;gt;http://www.raulparolari.com/Ruby2/define_method Defining dynamic methods &amp;lt;/ref&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby. &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of multiple inheritance in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself.&amp;lt;ref&amp;gt;http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby &amp;lt;/ref&amp;gt; One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&amp;lt;ref&amp;gt;http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and include &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53121</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53121"/>
		<updated>2011-10-20T18:11:49Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of uses of Meta Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
For [http://planet.plt-scheme.org/package-source/dherman/c.plt/3/1/planet-docs/c/index.html Meta programming in C-language], one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
For [http://www.boostpro.com/mplbook/ Meta programming in C++], it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code.&lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Prolog Prolog], a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called Ruby. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and vice versa. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
          end&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10) &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
Singleton classes are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. Also, we can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept. Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the module_eval method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using module_eval method:&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method:&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name “Xyz” in the program’s namespace and make sure it behaves like a normal class as if it is defined using the ‘class’ keyword.&lt;br /&gt;
The class can be instantiated and its attributes can be  accessed in the following way:&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the const_get method of class Object and the attribute is set and read using instance_variable_set and instance_variable_get methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The class_eval method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby. &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of multiple inheritance in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself. One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end          &lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                          # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
           Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
           Car.left 			        # NoMethodError &lt;br /&gt;
           Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53117</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53117"/>
		<updated>2011-10-20T18:05:01Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
In C-language, one of the popular examples of meta-programming is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
In C++, meta-programming is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code. &lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
Prolog, a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called Ruby. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and vice versa. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
          end&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10) &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
Singleton classes are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. Also, we can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept. Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the module_eval method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using module_eval method:&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method:&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name “Xyz” in the program’s namespace and make sure it behaves like a normal class as if it is defined using the ‘class’ keyword.&lt;br /&gt;
The class can be instantiated and its attributes can be  accessed in the following way:&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the const_get method of class Object and the attribute is set and read using instance_variable_set and instance_variable_get methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The class_eval method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby. &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of multiple inheritance in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself. One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                         # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
            Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
            Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
            Car.left 			        # NoMethodError &lt;br /&gt;
            Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53115</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53115"/>
		<updated>2011-10-20T18:04:17Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming].&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
In C-language, one of the popular examples of meta-programming is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
In C++, meta-programming is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code. &lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
Prolog, a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called Ruby. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and vice versa. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
          end&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10) &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
Singleton classes are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. Also, we can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept. Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the module_eval method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using module_eval method:&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method:&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name “Xyz” in the program’s namespace and make sure it behaves like a normal class as if it is defined using the ‘class’ keyword.&lt;br /&gt;
The class can be instantiated and its attributes can be  accessed in the following way:&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class                                     # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the const_get method of class Object and the attribute is set and read using instance_variable_set and instance_variable_get methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The class_eval method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		                        #  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		                        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname                                    # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname                                    # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby. &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of multiple inheritance in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself. One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def left&lt;br /&gt;
                 puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
              def self.right&lt;br /&gt;
                 puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
               include Actions&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           Car.new.left                         # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Car.new.right 			# NoMethodError&lt;br /&gt;
           Car.left         			# NoMethodError&lt;br /&gt;
    &lt;br /&gt;
           class Truck&lt;br /&gt;
              extend Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
           Truck.left 			        # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           Truck.right 		        	# NoMethodError&lt;br /&gt;
           Truck.new.left 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
           class Car&lt;br /&gt;
           end&lt;br /&gt;
           car = Car.new&lt;br /&gt;
           car.extend Actions&lt;br /&gt;
           car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
           car2 = Car.new&lt;br /&gt;
           car2.left 			        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&lt;br /&gt;
&lt;br /&gt;
           module Actions&lt;br /&gt;
              def self.included(base)&lt;br /&gt;
                 base.extend(ClassMethods)&lt;br /&gt;
              end&lt;br /&gt;
              def left&lt;br /&gt;
                 puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           module ClassMethods&lt;br /&gt;
              def right&lt;br /&gt;
                 puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           &lt;br /&gt;
           class Car&lt;br /&gt;
              include Actions&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
            Car.right 			        # &amp;quot;class method - right&amp;quot;&lt;br /&gt;
            Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
            Car.left 			        # NoMethodError &lt;br /&gt;
            Car.new.right 		        # NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53108</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53108"/>
		<updated>2011-10-20T17:50:03Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called Meta-programming.&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
In C-language, one of the popular examples of meta-programming is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
In C++, meta-programming is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code. &lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
Prolog, a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called Ruby. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and vice versa. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
          end&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10) &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
&lt;br /&gt;
=== Singleton classes ===&lt;br /&gt;
Singleton classes are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. Also, we can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept. Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
         class MyClass&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	    end&lt;br /&gt;
            def one&lt;br /&gt;
               puts &amp;quot;one&amp;quot;&lt;br /&gt;
            end&lt;br /&gt;
         end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
              def one     &lt;br /&gt;
                 puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
           def replace(array, from, to)&lt;br /&gt;
              array.each_with_index do |e i|&lt;br /&gt;
                 array[i] = to if e == from&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
&lt;br /&gt;
           class Array&lt;br /&gt;
              def replace(from,to)&lt;br /&gt;
                 self.each_with_index do |e, i|&lt;br /&gt;
                    self[i] = to if e == from&lt;br /&gt;
                 end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
           a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
           a.replace(2,0) &lt;br /&gt;
           puts a.to_s                         #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
               attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the module_eval method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using module_eval method:&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;		                             # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
            Object.module_eval(str)                                  # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method:&lt;br /&gt;
&lt;br /&gt;
            class_name = &amp;quot;Xyz&amp;quot;                                       # assume this is inputted by the user&lt;br /&gt;
            attr_name = &amp;quot;name&amp;quot;		                             # assume this is inputted by the user&lt;br /&gt;
            dynamic_class = Object.const_set(class_name, Class.new)  # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name “Xyz” in the program’s namespace and make sure it behaves like a normal class as if it is defined using the ‘class’ keyword.&lt;br /&gt;
The class can be instantiated and its attributes can be  accessed in the following way:&lt;br /&gt;
&lt;br /&gt;
            dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
            inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
            puts inst.class					    # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
            inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
            puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the const_get method of class Object and the attribute is set and read using instance_variable_set and instance_variable_get methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The class_eval method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
            attrs = [attr_name]&lt;br /&gt;
            dynamic_class.class_eval do&lt;br /&gt;
      	       attr_accessor *attrs&lt;br /&gt;
            end&lt;br /&gt;
            inst.name = &amp;quot;John&amp;quot;		#  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
            puts inst.name 		        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&lt;br /&gt;
        &lt;br /&gt;
            class Employee&lt;br /&gt;
               ATTRS = [:fname, :lname]&lt;br /&gt;
  	       def initialize *args  &lt;br /&gt;
                  @data = Hash.new&lt;br /&gt;
                  ATTRS.each_with_index do |attr, i|&lt;br /&gt;
                     @data[attr] = args[i]&lt;br /&gt;
      		  end&lt;br /&gt;
  	       end&lt;br /&gt;
               ATTRS.each do |attr|&lt;br /&gt;
                  define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
                     @data[attr.to_sym]&lt;br /&gt;
                  end	&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
            puts e.get_fname              # &amp;quot;John&amp;quot;&lt;br /&gt;
            puts e.get_lname              # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
            eval %{def get_#{attr}&lt;br /&gt;
               @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
            end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby. &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of multiple inheritance in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself. One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
module Actions&lt;br /&gt;
		def left&lt;br /&gt;
			puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
def self.right&lt;br /&gt;
		puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
class Car&lt;br /&gt;
	include Actions&lt;br /&gt;
end&lt;br /&gt;
Car.new.left 			# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
Car.new.right 			# NoMethodError&lt;br /&gt;
Car.left         			# NoMethodError&lt;br /&gt;
class Truck&lt;br /&gt;
	extend Actions&lt;br /&gt;
end&lt;br /&gt;
Truck.left 			 # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
Truck.right 			# NoMethodError&lt;br /&gt;
Truck.new.left 		# NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
class Car&lt;br /&gt;
end&lt;br /&gt;
car = Car.new&lt;br /&gt;
car.extend Actions&lt;br /&gt;
car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
car2 = Car.new&lt;br /&gt;
car2.left 			# NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&lt;br /&gt;
&lt;br /&gt;
module Actions&lt;br /&gt;
 	def self.included(base)&lt;br /&gt;
    		base.extend(ClassMethods)&lt;br /&gt;
	end&lt;br /&gt;
		def left&lt;br /&gt;
			puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
      	module ClassMethods&lt;br /&gt;
		def right&lt;br /&gt;
			puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
           end&lt;br /&gt;
class Car&lt;br /&gt;
  	include Actions&lt;br /&gt;
end&lt;br /&gt;
Car.right 			# &amp;quot;class method - right&amp;quot;&lt;br /&gt;
Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
Car.left 			# NoMethodError &lt;br /&gt;
Car.new.right 			# NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53099</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53099"/>
		<updated>2011-10-20T17:43:54Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called Meta-programming.&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
In C-language, one of the popular examples of meta-programming is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
In C++, meta-programming is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code. &lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
Prolog, a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called Ruby. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and vice versa. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
          end&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10) &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
&lt;br /&gt;
Singleton classes are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. Also, we can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept. Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
              puts ”This is the main method”&lt;br /&gt;
            end&lt;br /&gt;
            alias :method2 :method1&lt;br /&gt;
            def method1&lt;br /&gt;
               puts “This is not main method&amp;quot;&lt;br /&gt;
               method2&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
          a = A.new()&lt;br /&gt;
          a.method1&lt;br /&gt;
          =&amp;gt; This is not main method&lt;br /&gt;
          =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
 	class MyClass&lt;br /&gt;
          def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
          end&lt;br /&gt;
          def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	  end&lt;br /&gt;
          def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
        	  def one&lt;br /&gt;
                     puts &amp;quot;one&amp;quot;&lt;br /&gt;
        	  end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
            def replace(array, from, to)&lt;br /&gt;
	      array.each_with_index do |e i|&lt;br /&gt;
		array[i] = to if e == from&lt;br /&gt;
	      end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
            class Array&lt;br /&gt;
               def replace(from,to)&lt;br /&gt;
                  self.each_with_index do |e, i|&lt;br /&gt;
	          self[i] = to if e == from&lt;br /&gt;
	          end&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
            a.replace(2,0) &lt;br /&gt;
            puts a.to_s          #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
       		attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the module_eval method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using module_eval method:&lt;br /&gt;
&lt;br /&gt;
class_name = &amp;quot;Xyz&amp;quot;		                                    # assume this is inputted by the user&lt;br /&gt;
attr_name = &amp;quot;name&amp;quot;                                                    # assume this is inputted by the user&lt;br /&gt;
str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
Object.module_eval(str)                                             # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method:&lt;br /&gt;
&lt;br /&gt;
class_name = &amp;quot;Xyz&amp;quot;                     # assume this is inputted by the user&lt;br /&gt;
attr_name = &amp;quot;name&amp;quot;		    # assume this is inputted by the user&lt;br /&gt;
dynamic_class = Object.const_set(class_name, Class.new)     # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name “Xyz” in the program’s namespace and make sure it behaves like a normal class as if it is defined using the ‘class’ keyword.&lt;br /&gt;
The class can be instantiated and its attributes can be  accessed in the following way:&lt;br /&gt;
&lt;br /&gt;
        dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
        inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
        puts inst.class					    # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
        inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
        puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
        inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
        puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the const_get method of class Object and the attribute is set and read using instance_variable_set and instance_variable_get methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The class_eval method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
        attrs = [attr_name]&lt;br /&gt;
        dynamic_class.class_eval do&lt;br /&gt;
      	    attr_accessor *attrs&lt;br /&gt;
        end&lt;br /&gt;
        inst.name = &amp;quot;John&amp;quot;		#  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
        puts inst.name 		        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&lt;br /&gt;
        &lt;br /&gt;
        class Employee&lt;br /&gt;
            ATTRS = [:fname, :lname]&lt;br /&gt;
  	    def initialize *args  &lt;br /&gt;
      		@data = Hash.new&lt;br /&gt;
      		ATTRS.each_with_index do |attr, i|&lt;br /&gt;
         		@data[attr] = args[i]&lt;br /&gt;
      		end&lt;br /&gt;
  	    end&lt;br /&gt;
            ATTRS.each do |attr|&lt;br /&gt;
   		define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
         			@data[attr.to_sym]&lt;br /&gt;
    		end	&lt;br /&gt;
  	     end&lt;br /&gt;
         end&lt;br /&gt;
         e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
         puts e.get_fname              # &amp;quot;John&amp;quot;&lt;br /&gt;
         puts e.get_lname              # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
      eval %{def get_#{attr}&lt;br /&gt;
         @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
      end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby. &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of multiple inheritance in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself. One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
module Actions&lt;br /&gt;
		def left&lt;br /&gt;
			puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
def self.right&lt;br /&gt;
		puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
class Car&lt;br /&gt;
	include Actions&lt;br /&gt;
end&lt;br /&gt;
Car.new.left 			# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
Car.new.right 			# NoMethodError&lt;br /&gt;
Car.left         			# NoMethodError&lt;br /&gt;
class Truck&lt;br /&gt;
	extend Actions&lt;br /&gt;
end&lt;br /&gt;
Truck.left 			 # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
Truck.right 			# NoMethodError&lt;br /&gt;
Truck.new.left 		# NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
class Car&lt;br /&gt;
end&lt;br /&gt;
car = Car.new&lt;br /&gt;
car.extend Actions&lt;br /&gt;
car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
car2 = Car.new&lt;br /&gt;
car2.left 			# NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&lt;br /&gt;
&lt;br /&gt;
module Actions&lt;br /&gt;
 	def self.included(base)&lt;br /&gt;
    		base.extend(ClassMethods)&lt;br /&gt;
	end&lt;br /&gt;
		def left&lt;br /&gt;
			puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
      	module ClassMethods&lt;br /&gt;
		def right&lt;br /&gt;
			puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
           end&lt;br /&gt;
class Car&lt;br /&gt;
  	include Actions&lt;br /&gt;
end&lt;br /&gt;
Car.right 			# &amp;quot;class method - right&amp;quot;&lt;br /&gt;
Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
Car.left 			# NoMethodError &lt;br /&gt;
Car.new.right 			# NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53097</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53097"/>
		<updated>2011-10-20T17:41:34Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called Meta-programming.&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
In C-language, one of the popular examples of meta-programming is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
In C++, meta-programming is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code. &lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
Prolog, a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called Ruby. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and vice versa. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
          end&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10) &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
&lt;br /&gt;
Singleton classes are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&lt;br /&gt;
&lt;br /&gt;
        alias        :new  :old&lt;br /&gt;
        alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
     class Array&lt;br /&gt;
      def product&lt;br /&gt;
          inject {|a, x| a*x }&lt;br /&gt;
      end&lt;br /&gt;
      alias  :newproduct :product&lt;br /&gt;
     end&lt;br /&gt;
     a = Array.new([1, 2, 3, 4])&lt;br /&gt;
     a.newproduct   #=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. Also, we can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept. Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
          class A&lt;br /&gt;
            def method1&lt;br /&gt;
	      puts ”This is the main method”&lt;br /&gt;
	    end&lt;br /&gt;
	   alias :method2 :method1&lt;br /&gt;
	 &lt;br /&gt;
   	   def method1&lt;br /&gt;
	       puts “This is not main method”&lt;br /&gt;
	       method2&lt;br /&gt;
               end&lt;br /&gt;
	   end&lt;br /&gt;
           a = A.new()&lt;br /&gt;
           a.method1&lt;br /&gt;
           =&amp;gt; This is not main method&lt;br /&gt;
           =&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	    class MyClass&lt;br /&gt;
              def one&lt;br /&gt;
                puts &amp;quot;one&amp;quot;&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
 	class MyClass&lt;br /&gt;
          def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
          end&lt;br /&gt;
          def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	  end&lt;br /&gt;
          def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
        	  def one&lt;br /&gt;
                     puts &amp;quot;one&amp;quot;&lt;br /&gt;
        	  end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
            def replace(array, from, to)&lt;br /&gt;
	      array.each_with_index do |e i|&lt;br /&gt;
		array[i] = to if e == from&lt;br /&gt;
	      end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
            class Array&lt;br /&gt;
               def replace(from,to)&lt;br /&gt;
                  self.each_with_index do |e, i|&lt;br /&gt;
	          self[i] = to if e == from&lt;br /&gt;
	          end&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
            a.replace(2,0) &lt;br /&gt;
            puts a.to_s          #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
       		attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the module_eval method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using module_eval method:&lt;br /&gt;
&lt;br /&gt;
class_name = &amp;quot;Xyz&amp;quot;		                                    # assume this is inputted by the user&lt;br /&gt;
attr_name = &amp;quot;name&amp;quot;                                                    # assume this is inputted by the user&lt;br /&gt;
str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
Object.module_eval(str)                                             # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method:&lt;br /&gt;
&lt;br /&gt;
class_name = &amp;quot;Xyz&amp;quot;                     # assume this is inputted by the user&lt;br /&gt;
attr_name = &amp;quot;name&amp;quot;		    # assume this is inputted by the user&lt;br /&gt;
dynamic_class = Object.const_set(class_name, Class.new)     # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name “Xyz” in the program’s namespace and make sure it behaves like a normal class as if it is defined using the ‘class’ keyword.&lt;br /&gt;
The class can be instantiated and its attributes can be  accessed in the following way:&lt;br /&gt;
&lt;br /&gt;
        dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
        inst = dynamic_class.new                            # instantiate the class&lt;br /&gt;
        puts inst.class					    # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
        inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;)   # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
        puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
        inst.name = &amp;quot;John&amp;quot;                                  # error	&lt;br /&gt;
        puts inst.name                                      # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the const_get method of class Object and the attribute is set and read using instance_variable_set and instance_variable_get methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The class_eval method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
        attrs = [attr_name]&lt;br /&gt;
        dynamic_class.class_eval do&lt;br /&gt;
      	    attr_accessor *attrs&lt;br /&gt;
        end&lt;br /&gt;
        inst.name = &amp;quot;John&amp;quot;		#  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
        puts inst.name 		        # &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&lt;br /&gt;
        &lt;br /&gt;
        class Employee&lt;br /&gt;
            ATTRS = [:fname, :lname]&lt;br /&gt;
  	    def initialize *args  &lt;br /&gt;
      		@data = Hash.new&lt;br /&gt;
      		ATTRS.each_with_index do |attr, i|&lt;br /&gt;
         		@data[attr] = args[i]&lt;br /&gt;
      		end&lt;br /&gt;
  	    end&lt;br /&gt;
            ATTRS.each do |attr|&lt;br /&gt;
   		define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
         			@data[attr.to_sym]&lt;br /&gt;
    		end	&lt;br /&gt;
  	     end&lt;br /&gt;
         end&lt;br /&gt;
         e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
         puts e.get_fname              # &amp;quot;John&amp;quot;&lt;br /&gt;
         puts e.get_lname              # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
      eval %{def get_#{attr}&lt;br /&gt;
         @data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
      end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby. &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of multiple inheritance in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself. One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
module Actions&lt;br /&gt;
		def left&lt;br /&gt;
			puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
def self.right&lt;br /&gt;
		puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
class Car&lt;br /&gt;
	include Actions&lt;br /&gt;
end&lt;br /&gt;
Car.new.left 			# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
Car.new.right 			# NoMethodError&lt;br /&gt;
Car.left         			# NoMethodError&lt;br /&gt;
class Truck&lt;br /&gt;
	extend Actions&lt;br /&gt;
end&lt;br /&gt;
Truck.left 			 # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
Truck.right 			# NoMethodError&lt;br /&gt;
Truck.new.left 		# NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
class Car&lt;br /&gt;
end&lt;br /&gt;
car = Car.new&lt;br /&gt;
car.extend Actions&lt;br /&gt;
car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
car2 = Car.new&lt;br /&gt;
car2.left 			# NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&lt;br /&gt;
&lt;br /&gt;
module Actions&lt;br /&gt;
 	def self.included(base)&lt;br /&gt;
    		base.extend(ClassMethods)&lt;br /&gt;
	end&lt;br /&gt;
		def left&lt;br /&gt;
			puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
      	module ClassMethods&lt;br /&gt;
		def right&lt;br /&gt;
			puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
           end&lt;br /&gt;
class Car&lt;br /&gt;
  	include Actions&lt;br /&gt;
end&lt;br /&gt;
Car.right 			# &amp;quot;class method - right&amp;quot;&lt;br /&gt;
Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
Car.left 			# NoMethodError &lt;br /&gt;
Car.new.right 			# NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53093</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53093"/>
		<updated>2011-10-20T17:36:46Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called Meta-programming.&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
In C-language, one of the popular examples of meta-programming is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
In C++, meta-programming is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code. &lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
Prolog, a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called Ruby. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and vice versa. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
        &lt;br /&gt;
        a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
          end&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10) &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
&lt;br /&gt;
Singleton classes are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&lt;br /&gt;
&lt;br /&gt;
          a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
         a.prod&lt;br /&gt;
         =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&lt;br /&gt;
&lt;br /&gt;
        a = [1, 2, 3, 4]&lt;br /&gt;
        class Array&lt;br /&gt;
        class &amp;lt;&amp;lt; self&lt;br /&gt;
           def prod&lt;br /&gt;
               inject {|a, x| a*x}&lt;br /&gt;
           end&lt;br /&gt;
        end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&lt;br /&gt;
&lt;br /&gt;
	alias        :new  :old&lt;br /&gt;
	alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product	&lt;br /&gt;
         inject {|a, x| a*x }&lt;br /&gt;
       end&lt;br /&gt;
    alias  :newproduct :product&lt;br /&gt;
    end&lt;br /&gt;
    a = Array.new([1, 2, 3, 4])&lt;br /&gt;
    a.newproduct&lt;br /&gt;
    =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. Also, we can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept. Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
         class A&lt;br /&gt;
	    def method1&lt;br /&gt;
	      puts ”This is the main method”&lt;br /&gt;
	   end&lt;br /&gt;
	   alias :method2 :method1&lt;br /&gt;
	 &lt;br /&gt;
   	   def method1&lt;br /&gt;
	       puts “This is not main method”&lt;br /&gt;
	       method2&lt;br /&gt;
               end&lt;br /&gt;
	 end	&lt;br /&gt;
	a = A.new()&lt;br /&gt;
	a.method1&lt;br /&gt;
	=&amp;gt; This is not main method&lt;br /&gt;
	=&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	     class MyClass&lt;br /&gt;
       	        def one&lt;br /&gt;
	          puts &amp;quot;one&amp;quot;&lt;br /&gt;
        	end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
 	class MyClass&lt;br /&gt;
       	  def one&lt;br /&gt;
 	    puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	 end&lt;br /&gt;
       	 def one&lt;br /&gt;
	      puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	end&lt;br /&gt;
       	def one&lt;br /&gt;
	      puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
        	  def one&lt;br /&gt;
	            puts &amp;quot;one&amp;quot;&lt;br /&gt;
        	  end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
            def replace(array, from, to)&lt;br /&gt;
	      array.each_with_index do |e i|&lt;br /&gt;
		array[i] = to if e == from&lt;br /&gt;
	      end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
            class Array&lt;br /&gt;
               def replace(from,to)&lt;br /&gt;
                  self.each_with_index do |e, i|&lt;br /&gt;
	          self[i] = to if e == from&lt;br /&gt;
	          end&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
            a.replace(2,0) &lt;br /&gt;
            puts a.to_s          #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
       		attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the module_eval method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using module_eval method:&lt;br /&gt;
&lt;br /&gt;
class_name = &amp;quot;Xyz&amp;quot;		                                    # assume this is inputted by the user&lt;br /&gt;
attr_name = &amp;quot;name&amp;quot;                                                    # assume this is inputted by the user&lt;br /&gt;
str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
Object.module_eval(str)                                             # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method:&lt;br /&gt;
&lt;br /&gt;
class_name = &amp;quot;Xyz&amp;quot;                     # assume this is inputted by the user&lt;br /&gt;
attr_name = &amp;quot;name&amp;quot;		    # assume this is inputted by the user&lt;br /&gt;
dynamic_class = Object.const_set(class_name, Class.new)     # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name “Xyz” in the program’s namespace and make sure it behaves like a normal class as if it is defined using the ‘class’ keyword.&lt;br /&gt;
The class can be instantiated and its attributes can be  accessed in the following way:&lt;br /&gt;
&lt;br /&gt;
dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
inst = dynamic_class.new                                           # instantiate the class&lt;br /&gt;
puts inst.class					            # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;) # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
inst.name = &amp;quot;John&amp;quot;   # error	&lt;br /&gt;
puts inst.name          # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the const_get method of class Object and the attribute is set and read using instance_variable_set and instance_variable_get methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The class_eval method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
attrs = [attr_name]&lt;br /&gt;
dynamic_class.class_eval do&lt;br /&gt;
      	attr_accessor *attrs&lt;br /&gt;
end&lt;br /&gt;
inst.name = &amp;quot;John&amp;quot;		#  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
puts inst.name 		# &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&lt;br /&gt;
class Employee&lt;br /&gt;
	ATTRS = [:fname, :lname]&lt;br /&gt;
  	def initialize *args  &lt;br /&gt;
      		@data = Hash.new&lt;br /&gt;
      		ATTRS.each_with_index do |attr, i|&lt;br /&gt;
         		@data[attr] = args[i]&lt;br /&gt;
      		end&lt;br /&gt;
  	end&lt;br /&gt;
  	ATTRS.each do |attr|&lt;br /&gt;
   		define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
         			@data[attr.to_sym]&lt;br /&gt;
    		end	&lt;br /&gt;
  	end&lt;br /&gt;
end&lt;br /&gt;
e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
puts e.get_fname              # &amp;quot;John&amp;quot;&lt;br /&gt;
puts e.get_lname              # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
eval %{def get_#{attr}&lt;br /&gt;
	@data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby. &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of multiple inheritance in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself. One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
module Actions&lt;br /&gt;
		def left&lt;br /&gt;
			puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
def self.right&lt;br /&gt;
		puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
class Car&lt;br /&gt;
	include Actions&lt;br /&gt;
end&lt;br /&gt;
Car.new.left 			# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
Car.new.right 			# NoMethodError&lt;br /&gt;
Car.left         			# NoMethodError&lt;br /&gt;
class Truck&lt;br /&gt;
	extend Actions&lt;br /&gt;
end&lt;br /&gt;
Truck.left 			 # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
Truck.right 			# NoMethodError&lt;br /&gt;
Truck.new.left 		# NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
class Car&lt;br /&gt;
end&lt;br /&gt;
car = Car.new&lt;br /&gt;
car.extend Actions&lt;br /&gt;
car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
car2 = Car.new&lt;br /&gt;
car2.left 			# NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&lt;br /&gt;
&lt;br /&gt;
module Actions&lt;br /&gt;
 	def self.included(base)&lt;br /&gt;
    		base.extend(ClassMethods)&lt;br /&gt;
	end&lt;br /&gt;
		def left&lt;br /&gt;
			puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
      	module ClassMethods&lt;br /&gt;
		def right&lt;br /&gt;
			puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
           end&lt;br /&gt;
class Car&lt;br /&gt;
  	include Actions&lt;br /&gt;
end&lt;br /&gt;
Car.right 			# &amp;quot;class method - right&amp;quot;&lt;br /&gt;
Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
Car.left 			# NoMethodError &lt;br /&gt;
Car.new.right 			# NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53091</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53091"/>
		<updated>2011-10-20T17:33:01Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called Meta-programming.&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
In C-language, one of the popular examples of meta-programming is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
In C++, meta-programming is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code. &lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
Prolog, a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called Ruby. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and vice versa. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
    a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
&lt;br /&gt;
      	a.product&lt;br /&gt;
        =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&lt;br /&gt;
 &lt;br /&gt;
    # The Rectangle constructor accepts arguments in either&lt;br /&gt;
    # of the following forms:&lt;br /&gt;
    class Rectangle&lt;br /&gt;
       def initialize(*args)&lt;br /&gt;
          if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
             puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
          else&lt;br /&gt;
             puts 'Correct number of arguments passed!!!'&lt;br /&gt;
          end&lt;br /&gt;
       end&lt;br /&gt;
    end&lt;br /&gt;
    Rectangle.new([10, 23], 4, 10) &lt;br /&gt;
    Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
&lt;br /&gt;
Singleton classes are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&lt;br /&gt;
&lt;br /&gt;
	a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
 Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
     class &amp;lt;&amp;lt; a&lt;br /&gt;
       def prod&lt;br /&gt;
         inject{|a, x| a*x}&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
		a.prod&lt;br /&gt;
 		=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&lt;br /&gt;
&lt;br /&gt;
	a = [1, 2, 3, 4]&lt;br /&gt;
	class Array&lt;br /&gt;
	class &amp;lt;&amp;lt; self&lt;br /&gt;
	   def prod&lt;br /&gt;
		inject {|a, x| a*x}&lt;br /&gt;
	   end&lt;br /&gt;
	end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&lt;br /&gt;
&lt;br /&gt;
    Object.class_eval do&lt;br /&gt;
      class &amp;lt;&amp;lt; self&lt;br /&gt;
        def attribute_accessor( *instance_variables )&lt;br /&gt;
          instance_variables.each do |instance_variable|&lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
   	    def #{instance_variable}&lt;br /&gt;
               @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
               @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ? &lt;br /&gt;
  	  end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&lt;br /&gt;
&lt;br /&gt;
	alias        :new  :old&lt;br /&gt;
	alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
    class Array&lt;br /&gt;
       def product	&lt;br /&gt;
         inject {|a, x| a*x }&lt;br /&gt;
       end&lt;br /&gt;
    alias  :newproduct :product&lt;br /&gt;
    end&lt;br /&gt;
    a = Array.new([1, 2, 3, 4])&lt;br /&gt;
    a.newproduct&lt;br /&gt;
    =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. Also, we can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept. Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
         class A&lt;br /&gt;
	    def method1&lt;br /&gt;
	      puts ”This is the main method”&lt;br /&gt;
	   end&lt;br /&gt;
	   alias :method2 :method1&lt;br /&gt;
	 &lt;br /&gt;
   	   def method1&lt;br /&gt;
	       puts “This is not main method”&lt;br /&gt;
	       method2&lt;br /&gt;
               end&lt;br /&gt;
	 end	&lt;br /&gt;
	a = A.new()&lt;br /&gt;
	a.method1&lt;br /&gt;
	=&amp;gt; This is not main method&lt;br /&gt;
	=&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	     class MyClass&lt;br /&gt;
       	        def one&lt;br /&gt;
	          puts &amp;quot;one&amp;quot;&lt;br /&gt;
        	end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
 	class MyClass&lt;br /&gt;
       	  def one&lt;br /&gt;
 	    puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	 end&lt;br /&gt;
       	 def one&lt;br /&gt;
	      puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	end&lt;br /&gt;
       	def one&lt;br /&gt;
	      puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
        	  def one&lt;br /&gt;
	            puts &amp;quot;one&amp;quot;&lt;br /&gt;
        	  end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
            def replace(array, from, to)&lt;br /&gt;
	      array.each_with_index do |e i|&lt;br /&gt;
		array[i] = to if e == from&lt;br /&gt;
	      end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
            class Array&lt;br /&gt;
               def replace(from,to)&lt;br /&gt;
                  self.each_with_index do |e, i|&lt;br /&gt;
	          self[i] = to if e == from&lt;br /&gt;
	          end&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
            a.replace(2,0) &lt;br /&gt;
            puts a.to_s          #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
       		attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the module_eval method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using module_eval method:&lt;br /&gt;
&lt;br /&gt;
class_name = &amp;quot;Xyz&amp;quot;		                                    # assume this is inputted by the user&lt;br /&gt;
attr_name = &amp;quot;name&amp;quot;                                                    # assume this is inputted by the user&lt;br /&gt;
str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
Object.module_eval(str)                                             # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method:&lt;br /&gt;
&lt;br /&gt;
class_name = &amp;quot;Xyz&amp;quot;                     # assume this is inputted by the user&lt;br /&gt;
attr_name = &amp;quot;name&amp;quot;		    # assume this is inputted by the user&lt;br /&gt;
dynamic_class = Object.const_set(class_name, Class.new)     # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name “Xyz” in the program’s namespace and make sure it behaves like a normal class as if it is defined using the ‘class’ keyword.&lt;br /&gt;
The class can be instantiated and its attributes can be  accessed in the following way:&lt;br /&gt;
&lt;br /&gt;
dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
inst = dynamic_class.new                                           # instantiate the class&lt;br /&gt;
puts inst.class					            # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;) # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
inst.name = &amp;quot;John&amp;quot;   # error	&lt;br /&gt;
puts inst.name          # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the const_get method of class Object and the attribute is set and read using instance_variable_set and instance_variable_get methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The class_eval method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
attrs = [attr_name]&lt;br /&gt;
dynamic_class.class_eval do&lt;br /&gt;
      	attr_accessor *attrs&lt;br /&gt;
end&lt;br /&gt;
inst.name = &amp;quot;John&amp;quot;		#  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
puts inst.name 		# &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&lt;br /&gt;
class Employee&lt;br /&gt;
	ATTRS = [:fname, :lname]&lt;br /&gt;
  	def initialize *args  &lt;br /&gt;
      		@data = Hash.new&lt;br /&gt;
      		ATTRS.each_with_index do |attr, i|&lt;br /&gt;
         		@data[attr] = args[i]&lt;br /&gt;
      		end&lt;br /&gt;
  	end&lt;br /&gt;
  	ATTRS.each do |attr|&lt;br /&gt;
   		define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
         			@data[attr.to_sym]&lt;br /&gt;
    		end	&lt;br /&gt;
  	end&lt;br /&gt;
end&lt;br /&gt;
e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
puts e.get_fname              # &amp;quot;John&amp;quot;&lt;br /&gt;
puts e.get_lname              # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
eval %{def get_#{attr}&lt;br /&gt;
	@data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby. &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of multiple inheritance in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself. One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
module Actions&lt;br /&gt;
		def left&lt;br /&gt;
			puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
def self.right&lt;br /&gt;
		puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
class Car&lt;br /&gt;
	include Actions&lt;br /&gt;
end&lt;br /&gt;
Car.new.left 			# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
Car.new.right 			# NoMethodError&lt;br /&gt;
Car.left         			# NoMethodError&lt;br /&gt;
class Truck&lt;br /&gt;
	extend Actions&lt;br /&gt;
end&lt;br /&gt;
Truck.left 			 # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
Truck.right 			# NoMethodError&lt;br /&gt;
Truck.new.left 		# NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
class Car&lt;br /&gt;
end&lt;br /&gt;
car = Car.new&lt;br /&gt;
car.extend Actions&lt;br /&gt;
car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
car2 = Car.new&lt;br /&gt;
car2.left 			# NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&lt;br /&gt;
&lt;br /&gt;
module Actions&lt;br /&gt;
 	def self.included(base)&lt;br /&gt;
    		base.extend(ClassMethods)&lt;br /&gt;
	end&lt;br /&gt;
		def left&lt;br /&gt;
			puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
      	module ClassMethods&lt;br /&gt;
		def right&lt;br /&gt;
			puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
           end&lt;br /&gt;
class Car&lt;br /&gt;
  	include Actions&lt;br /&gt;
end&lt;br /&gt;
Car.right 			# &amp;quot;class method - right&amp;quot;&lt;br /&gt;
Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
Car.left 			# NoMethodError &lt;br /&gt;
Car.new.right 			# NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53088</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_as&amp;diff=53088"/>
		<updated>2011-10-20T17:25:48Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: Created page with &amp;quot;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page, the concept of Meta-programming in Ruby is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of Meta-program in general i.e., its evolution and implementation in various languages is discussed. We then, discuss about the implementation of the concept of meta-programming in a dynamic and object-oriented programming language called Ruby.&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
A program that manipulates its code or other program’s code as its data is called a meta-program. An established example of meta-program is a compiler. The act of writing these meta-programs, that write or manipulate other programs or themselves as data is called Meta-programming.&lt;br /&gt;
We can also define meta-program as code generating program and writing such programs is called meta-programming.&lt;br /&gt;
&lt;br /&gt;
This allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.&lt;br /&gt;
&lt;br /&gt;
The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.&lt;br /&gt;
&lt;br /&gt;
==Uses of Meta-programming==&lt;br /&gt;
Let us look at some of the uses of meta-programming:&lt;br /&gt;
&lt;br /&gt;
1)	The major use of meta-programming is that it pre-generates the code at run-time. For example if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup of runtime.&lt;br /&gt;
&lt;br /&gt;
2)	It is the key ingradiant for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.&lt;br /&gt;
&lt;br /&gt;
3)	Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code. Also the maintenance becomes easy.&lt;br /&gt;
&lt;br /&gt;
4)	Also, we can use meta-programming for building frameworks and thus perform web application development. Ruby on Rails is one such example. Ruby’s meta-programming feature is the key feature for developing web application in Rails.&lt;br /&gt;
&lt;br /&gt;
== History of Meta-programming ==&lt;br /&gt;
Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.&lt;br /&gt;
 &lt;br /&gt;
In C-language, one of the popular examples of meta-programming is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C. Instead of doing the laborious work of generating a parser in C, one can use Yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.&lt;br /&gt;
&lt;br /&gt;
In C++, meta-programming is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written inbetween question marks. Usually this code is quite complicated and consists of a template and some instructions on how to fill the code. &lt;br /&gt;
&lt;br /&gt;
Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.&lt;br /&gt;
Prolog, a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.&lt;br /&gt;
&lt;br /&gt;
== Meta programming in Ruby == &lt;br /&gt;
In this column, we will discuss about the implementation of meta-programming in a programming language called Ruby. We will also discuss about various techniques which help in implementing meta-programming. Ruby is an object-oriented and dynamic language. Ruby is also reflexive i.e., Ruby program can observe and modify its own structure and behavior at runtime. Also in Ruby, code is nothing but data and vice versa. All these features of Ruby make it implement one of its most powerful features called meta-programming efficiently.&lt;br /&gt;
&lt;br /&gt;
In Ruby, with this meta-programming feature, we can write code that manipulates language constructs like instance variables, classes and modules at runtime. One of the major applications of Ruby is in Rails. Rails is a framework, which can be used for web applications development. The feature of meta-programming in Ruby is the key in building this rails framework. &lt;br /&gt;
&lt;br /&gt;
Open class property of Ruby helps it to enhance the implement of the concept of meta-programming efficiently. In Ruby, the classes (both standard classes and the classes created by us) are never closed i.e., we can always add extra methods to an existing class. All we need to do is open the class to which we want to add additional methods and then define the method we want to add. For instance, consider the following example:&lt;br /&gt;
&lt;br /&gt;
a = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
Here ‘a’ is an array and if we want to find the product of all the elements of the array, we need to open the Array class and add the method to calculate the product of the array elements.&lt;br /&gt;
&lt;br /&gt;
class Array&lt;br /&gt;
   def product&lt;br /&gt;
        inject{|a,x| a*x}&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
We can now call method product for the array.&lt;br /&gt;
	a.product&lt;br /&gt;
            =&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Generally we add the methods to the classes rather than the class objects, because adding to class objects will make the methods visible and thereby affecting the encapsulation. Sometimes adding a method can override already existing method. So, it is sometimes better to create a subclass to the existing class and add the method to the subclass.&lt;br /&gt;
&lt;br /&gt;
In Ruby, we can show as if there were two methods with same name. With this property, we can implement the concept of meta-programming efficiently in Ruby. Suppose, we have a class called Rectangle and it has an initialize method. Here, we can now instantiate this Rectangle by two ways. That is, either by passing in the coordinates of its corners or by passing in any of its corner along with the length and width of the corner. So, in this manner, even though there is only one initialize method, we can act as if there were two.&lt;br /&gt;
&lt;br /&gt;
# The Rectangle constructor accepts arguments in either&lt;br /&gt;
# of the following forms:&lt;br /&gt;
class Rectangle&lt;br /&gt;
  def initialize(*args)&lt;br /&gt;
      if args.size &amp;lt; 2  || args.size &amp;gt; 3&lt;br /&gt;
         puts 'Sorry wrong number of arguments passed. This method takes either 2 or 3 arguments.'  &lt;br /&gt;
     else&lt;br /&gt;
          puts 'Correct number of arguments passed!!!'&lt;br /&gt;
      end&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
Rectangle.new([10, 23], 4, 10)&lt;br /&gt;
Rectangle.new([10, 23], [14, 13])&lt;br /&gt;
&lt;br /&gt;
Singleton classes are one of the major properties of Ruby which make implementation of meta-programming efficient. Singleton class also called as meta-class or anonymous class is a class that acts as a proxy to the objects of a class. So firstly, let us understand what a singleton class means and then we will discuss about how it enhances the implementation of meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
In Ruby, any object will have a class of which it is an instance (the class of the object can be found by calling the method class on it). Ruby has a feature where we can add additional methods to an object. So, when this is done, a new anonymous class is created which acts as an intermediate between the object and its actual class. This intermediate class or proxy class is called as a singleton class. So, whenever the additional method defined for the object is called, the singleton class is searched first and then the actual class is searched (for the implementation).  Let us look at the following example to understand this. Consider an array object a defined as follows:&lt;br /&gt;
&lt;br /&gt;
	a= [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
 Now, if we want to add a new method to this object which will find out the product of all the elements in the array, we can do it as follows:&lt;br /&gt;
&lt;br /&gt;
   class &amp;lt;&amp;lt; a&lt;br /&gt;
   def prod&lt;br /&gt;
     inject{|a, x| a*x}&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Now we can call,&lt;br /&gt;
&lt;br /&gt;
		a.prod&lt;br /&gt;
 		=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
Here, when the method prod is called by object a, a singleton class is created between the object and its actual class. This class is hidden and has no name. Also, we cannot instantiate a new object from this hidden anonymous class.&lt;br /&gt;
&lt;br /&gt;
Now, let us discuss about another aspect of singleton classes. As we know all the objects can have a singleton class. Also, any class is actually an object of itself and also of the class Class. That is, the class Array is an object of itself and also of class Class. Suppose that, we define a method called prod just as above for this class Array, that method will actually be executed in the scope of the Array instance of the class Class (since Array is an object of Class class). The following is an example illustrating this:&lt;br /&gt;
&lt;br /&gt;
	a = [1, 2, 3, 4]&lt;br /&gt;
	class Array&lt;br /&gt;
	class &amp;lt;&amp;lt; self&lt;br /&gt;
	   def prod&lt;br /&gt;
		inject {|a, x| a*x}&lt;br /&gt;
	 end&lt;br /&gt;
	end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
Here, the method prod will be defined in the singleton class of String (since String class is considered as an object of Class class). Here we are using the variable self because it always refers to the current object (which is by default the receiver of the method call). So here, self refers to the current object of the class Class i.e., Array.&lt;br /&gt;
&lt;br /&gt;
Now let us look at the use of these singleton classes. The addition of the methods to instances and classes (as discussed above) are some of the uses of singleton classes. Various tricks/methods of Ruby like class_eval, module_eval, instance_variables etc., when invoked on singleton classes will be simple yet powerful in terms of efficiency of implementation of meta-programming, rather than invoking those tricks/methods on self. The following example in which, some of the above mentioned tricks/methods are invoked on a singleton class of an object will illustrate this advantage:&lt;br /&gt;
&lt;br /&gt;
Object.class_eval do&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
def attribute_accessor( *instance_variables )&lt;br /&gt;
instance_variables.each do |instance_variable|&lt;br /&gt;
        	      class_eval %Q?&lt;br /&gt;
   	 def #{instance_variable}&lt;br /&gt;
@#{instance_variable}&lt;br /&gt;
end&lt;br /&gt;
def #{instance_variable}=( new_value )&lt;br /&gt;
@#{instance_variable} = new_value&lt;br /&gt;
end&lt;br /&gt;
   	      ? &lt;br /&gt;
  	     end&lt;br /&gt;
end&lt;br /&gt;
    end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
Here, we can see that a singleton class is being created for any object. So, we use this class to redefine methods and also to add new methods. For instance, we can add new class methods by using the class_eval method (trick) without actually typing the class name explicitly. Also, we can get to know the name of the class the object belongs to by calling the method instance_variable. In this manner we can use all these methods in singleton class and modify methods and variables without actually typing the class name. &lt;br /&gt;
&lt;br /&gt;
=== Method aliasing ===&lt;br /&gt;
Method aliasing is a technique used in Ruby to implement the concept of meta-programming. The technique that allows us to give aliases or new names to already existing variables, operators and methods is called as method aliasing. It can also be used to wrap an existing method effectively by intercepting any calls and injecting the desired behavior. Here the new reference or the aliasing name may not be a local, instance, constant, or class variable if we are aliasing a variable.&lt;br /&gt;
&lt;br /&gt;
Using this method or technique we can perform method over riding efficiently and there by change the behavior of an object or a class. Also, we can use aliasing for more expressive options to the programmer using the class. That is, we can provide a second name to a method or variable which can be used outside the class too. There are two keywords in Ruby which provide this method aliasing. They are alias and alias_method. The following is the syntax for both these keywords:&lt;br /&gt;
&lt;br /&gt;
	alias        :new  :old&lt;br /&gt;
	alias_method :new, :old&lt;br /&gt;
&lt;br /&gt;
Generally we use alias to give a second name to a method or variable of a class, whereas we use alias_method when we want to assign second name to a method in a Module. So basically, alias keyword takes two arguments as we can see in the syntax. The first argument is the new name or the second name and second argument is the old name or the original name. Generally, we use labels to refer to the methods and the variables for which aliasing is being done.&lt;br /&gt;
Let us look at an example that illustrates this technique. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
class Array&lt;br /&gt;
  def product	&lt;br /&gt;
inject {|a, x| a*x }&lt;br /&gt;
  end&lt;br /&gt;
 alias  :newproduct :product&lt;br /&gt;
end&lt;br /&gt;
a = Array.new([1, 2, 3, 4])&lt;br /&gt;
a.newproduct&lt;br /&gt;
=&amp;gt; 24&lt;br /&gt;
&lt;br /&gt;
In this example, we are defining a new method called product and then aliasing that method with a new name called newproduct in the class Array. Now we can call this method with its alias name as above and still the method will be invoked. This is how method aliasing can be implementing. Also, we can notice here that, this technique can be used to add new methods and alias those methods for a given class. In this manner we can also change the behavior of the class. Also, we can call the aliasing name of a method inside another method thereby implementing method over riding concept. Consider the following example to illustrate this:&lt;br /&gt;
&lt;br /&gt;
	class A&lt;br /&gt;
	    def method1&lt;br /&gt;
	      puts ”This is the main method”&lt;br /&gt;
	   end&lt;br /&gt;
	   alias :method2 :method1&lt;br /&gt;
	 &lt;br /&gt;
   	   def method1&lt;br /&gt;
	       puts “This is not main method”&lt;br /&gt;
	       method2&lt;br /&gt;
               end&lt;br /&gt;
	end	&lt;br /&gt;
	&lt;br /&gt;
	a = A.new()&lt;br /&gt;
	a.method1&lt;br /&gt;
	=&amp;gt; This is not main method&lt;br /&gt;
	=&amp;gt; This is main method	&lt;br /&gt;
&lt;br /&gt;
So, in this manner method over riding is done using method aliasing. We can thus say that method aliasing can be used to implement the concept of meta-programming. Also, using method aliasing enables Ruby to implement Aspect Oriented Programming and its libraries.&lt;br /&gt;
&lt;br /&gt;
=== Class Definitions === &lt;br /&gt;
In statically typed languages, the classes are defined at compile time and used by the program at runtime. But dynamically typed languages do not consider this distinction. In Ruby, the class definitions are expressions and they are evaluated at run time just like any other code. So, the classes and its attributes and even methods can be defined at runtime. &lt;br /&gt;
Look at the following example:&lt;br /&gt;
&lt;br /&gt;
          3.times do&lt;br /&gt;
     	     class MyClass&lt;br /&gt;
       	        def one&lt;br /&gt;
	          puts &amp;quot;one&amp;quot;&lt;br /&gt;
        	end&lt;br /&gt;
              end&lt;br /&gt;
           end&lt;br /&gt;
&lt;br /&gt;
This code is evaluated at runtime and so it defines the class MyClass three times. You might think that it is invalid to define the same class several times. But remember, when Ruby comes across the definition of a class which already have been defined earlier, it just opens the class definition and appends the code in the new definition to the code in the old definition. So, the definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
 	class MyClass&lt;br /&gt;
       	  def one&lt;br /&gt;
 	    puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	 end&lt;br /&gt;
       	 def one&lt;br /&gt;
	      puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	end&lt;br /&gt;
       	def one&lt;br /&gt;
	      puts &amp;quot;one&amp;quot;&lt;br /&gt;
       	end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
But as you can see the same method is defined three times. Ruby replaces the old definition of a method with the new one every time it comes across a new definition of the same method. So the final definition of class MyClass will look like as follows:&lt;br /&gt;
&lt;br /&gt;
           class MyClass&lt;br /&gt;
        	  def one&lt;br /&gt;
	            puts &amp;quot;one&amp;quot;&lt;br /&gt;
        	  end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to add methods to an existing class at runtime. Suppose we want to define a method which replaces the elements with a certain value in an array with another value. It can be done by defining a replace method as follows: &lt;br /&gt;
&lt;br /&gt;
            def replace(array, from, to)&lt;br /&gt;
	      array.each_with_index do |e i|&lt;br /&gt;
		array[i] = to if e == from&lt;br /&gt;
	      end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
But this is not an object-oriented approach as this method has nothing to do with the class in which the replace method is defined. The replace method is in fact dealing with the elements of an array. So, the object-oriented approach will be to define the replace method in the class Array. But the class Array is already defined in Ruby, so we can add the replace method by simply defining the class Array again with the replace method definition in it as follows:&lt;br /&gt;
            class Array&lt;br /&gt;
               def replace(from,to)&lt;br /&gt;
                  self.each_with_index do |e, i|&lt;br /&gt;
	          self[i] = to if e == from&lt;br /&gt;
	          end&lt;br /&gt;
               end&lt;br /&gt;
            end&lt;br /&gt;
            a = [1,2,3,4,5,6,2,3,4,5,2,3,2]&lt;br /&gt;
            a.replace(2,0) &lt;br /&gt;
            puts a.to_s          #  [1, 0, 3, 4, 5, 6, 0, 3, 4, 5, 0, 3, 0]&lt;br /&gt;
&lt;br /&gt;
But there is a problem in defining the replace method for the class Array. The class Array already has a built-in replace method. So, when we define the new replace method, Ruby replaces the built-in replace method with the new one and so the original implementation of replace method is lost.  &lt;br /&gt;
&lt;br /&gt;
Now we will look at how a new class and it attributes are defined at runtime. This is useful when the programmer doesn't know what will be the names of the class and its attributes until runtime.&lt;br /&gt;
In Ruby, a class and its attributes are defined statically as follows:&lt;br /&gt;
&lt;br /&gt;
            class Xyz&lt;br /&gt;
       		attr_accessor :name&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
In Ruby, one way to define a class and its attributes at runtime is to use the module_eval method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using module_eval method:&lt;br /&gt;
&lt;br /&gt;
class_name = &amp;quot;Xyz&amp;quot;		                                    # assume this is inputted by the user&lt;br /&gt;
attr_name = &amp;quot;name&amp;quot;                                                    # assume this is inputted by the user&lt;br /&gt;
str = (&amp;quot;class #{class_name}; #{attr_name}; end&amp;quot;)&lt;br /&gt;
Object.module_eval(str)                                             # create the class&lt;br /&gt;
&lt;br /&gt;
In the above code, the class_name and attr_name variables contain the class name and the attribute name which are provided by the user at runtime. The module_eval evaluates the string formed for defining the class and  creates the class using the value contained by class_name variable and an attribute using the value contained by attr_name variable. It  adds the class name as a constant in the program's namespace.&lt;br /&gt;
An another way to define a class and its attributes at runtime  is to use const_set method of class Object.&lt;br /&gt;
Let us see an example of how the above class can be defined using const_set method:&lt;br /&gt;
&lt;br /&gt;
class_name = &amp;quot;Xyz&amp;quot;                     # assume this is inputted by the user&lt;br /&gt;
attr_name = &amp;quot;name&amp;quot;		    # assume this is inputted by the user&lt;br /&gt;
dynamic_class = Object.const_set(class_name, Class.new)     # create the class&lt;br /&gt;
&lt;br /&gt;
Here, the const_set method adds the name “Xyz” in the program’s namespace and make sure it behaves like a normal class as if it is defined using the ‘class’ keyword.&lt;br /&gt;
The class can be instantiated and its attributes can be  accessed in the following way:&lt;br /&gt;
&lt;br /&gt;
dynamic_class = Object.const_get(class_name)        # get the class&lt;br /&gt;
inst = dynamic_class.new                                           # instantiate the class&lt;br /&gt;
puts inst.class					            # &amp;quot;Xyz&amp;quot;&lt;br /&gt;
inst.instance_variable_set(&amp;quot;@&amp;quot;+attr_name, &amp;quot;John&amp;quot;) # set @name to &amp;quot;John&amp;quot;&lt;br /&gt;
puts inst.instance_variable_get(&amp;quot;@&amp;quot;+attr_name)      # &amp;quot;John&amp;quot;  &lt;br /&gt;
inst.name = &amp;quot;John&amp;quot;   # error	&lt;br /&gt;
puts inst.name          # error&lt;br /&gt;
&lt;br /&gt;
The class is instantiated using new method but first the class is retrieved from progam's namespace using the const_get method of class Object and the attribute is set and read using instance_variable_set and instance_variable_get methods. These methods require to have the sing '@' before the attribute name unlike the attribute accessors which use symbol.&lt;br /&gt;
If the attribute 'name' is directly accessed, it will give an error as the attribute accessor methods are not defined for the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
But, the attribute accessors can be explicitly defined for the attributes using the class_eval method. The class_eval method executes the code between the 'do' and 'end' as if it is written in the static class definition. The class_eval method can also be used to define methods at runtime.&lt;br /&gt;
&lt;br /&gt;
attrs = [attr_name]&lt;br /&gt;
dynamic_class.class_eval do&lt;br /&gt;
      	attr_accessor *attrs&lt;br /&gt;
end&lt;br /&gt;
inst.name = &amp;quot;John&amp;quot;		#  set name to &amp;quot;John&amp;quot;&lt;br /&gt;
puts inst.name 		# &amp;quot;John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The attrs is an array which contains all the attributes of the class. The method class_eval executes the line 'attr_accessor *attrs' as if it is defined in the class definition. This causes to define accessor methods for each of the attribute contained in the attrs array. So, the attribute 'name' can be accessed directly.&lt;br /&gt;
&lt;br /&gt;
Now we will look at how to define new methods for a class at runtime. In Ruby, the methods can be generated dynamically using define_method or using def inside of an eval. &lt;br /&gt;
&lt;br /&gt;
The define_method is a private class method and so it must be invoked from within the class context. Its parameters are the method name which can be either a string, a symbol or a variable and the block which becomes the method body.&lt;br /&gt;
class Employee&lt;br /&gt;
	ATTRS = [:fname, :lname]&lt;br /&gt;
  	def initialize *args  &lt;br /&gt;
      		@data = Hash.new&lt;br /&gt;
      		ATTRS.each_with_index do |attr, i|&lt;br /&gt;
         		@data[attr] = args[i]&lt;br /&gt;
      		end&lt;br /&gt;
  	end&lt;br /&gt;
  	ATTRS.each do |attr|&lt;br /&gt;
   		define_method &amp;quot;get_#{attr}&amp;quot;  do&lt;br /&gt;
         			@data[attr.to_sym]&lt;br /&gt;
    		end	&lt;br /&gt;
  	end&lt;br /&gt;
end&lt;br /&gt;
e = Employee.new(&amp;quot;John&amp;quot;, &amp;quot;Smith&amp;quot;)&lt;br /&gt;
puts e.get_fname              # &amp;quot;John&amp;quot;&lt;br /&gt;
puts e.get_lname              # &amp;quot;Smith&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This could have been done using def keyword also. But there are two important benefits using define_method. First, we can define the method name dynamically. Second one is that the define_method accepts a block as one parameter which is in fact a closure in Ruby. &lt;br /&gt;
The another way to define method dynamically is using def inside an eval. &lt;br /&gt;
&lt;br /&gt;
eval %{def get_#{attr}&lt;br /&gt;
	@data[&amp;quot;#{attr}&amp;quot;.to_sym]&lt;br /&gt;
end}&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
Modules are a way of grouping together methods, constants, classes and other modules in Ruby. &lt;br /&gt;
They are defined using the keyword module. The modules cannot be instantiated nor they can be sub classed. But the modules can be added to the other classes so that the classes can make use of the functionality defined in the modules. &lt;br /&gt;
There are particularly two uses of modules - &lt;br /&gt;
&lt;br /&gt;
They act as namespace and prevent the name clashes that are generally caused by same method names or variable names in different classes.&lt;br /&gt;
The other use is that they share the functionality between classes. A module can be mixed in a class so that functionality defined in that module becomes available to the class at runtime. Modules serve the purpose of multiple inheritance in Ruby indirectly as multiple modules can be mixed in a class. &lt;br /&gt;
&lt;br /&gt;
Either 'include' or 'extend' can be used to mix in a module’s functionality into a class, 'include' is used to add the module’s methods to the instance of a class, while 'extend' is used to add the module's methods to the class itself. One thing it should be noted that only the instance methods of the module are added to the class, no matter either 'include' or 'extend' is used. &lt;br /&gt;
&lt;br /&gt;
module Actions&lt;br /&gt;
		def left&lt;br /&gt;
			puts '&amp;quot;Move to left&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
def self.right&lt;br /&gt;
		puts &amp;quot;Move to right&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
class Car&lt;br /&gt;
	include Actions&lt;br /&gt;
end&lt;br /&gt;
Car.new.left 			# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
Car.new.right 			# NoMethodError&lt;br /&gt;
Car.left         			# NoMethodError&lt;br /&gt;
class Truck&lt;br /&gt;
	extend Actions&lt;br /&gt;
end&lt;br /&gt;
Truck.left 			 # &amp;quot;Move to left&amp;quot;&lt;br /&gt;
Truck.right 			# NoMethodError&lt;br /&gt;
Truck.new.left 		# NoMethodError&lt;br /&gt;
&lt;br /&gt;
The module's methods can added to a particular instance of  the class also.&lt;br /&gt;
&lt;br /&gt;
class Car&lt;br /&gt;
end&lt;br /&gt;
car = Car.new&lt;br /&gt;
car.extend Actions&lt;br /&gt;
car.left				# &amp;quot;Move to left&amp;quot;&lt;br /&gt;
car2 = Car.new&lt;br /&gt;
car2.left 			# NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the methods of module 'Actions' are added to the instance 'car' but they are not added to the instance 'car2' as only instance 'car' extends the module 'Actions'.&lt;br /&gt;
&lt;br /&gt;
If you want to add the class methods of a module to a class, then it can be done by making use of Module#included method.&lt;br /&gt;
&lt;br /&gt;
module Actions&lt;br /&gt;
 	def self.included(base)&lt;br /&gt;
    		base.extend(ClassMethods)&lt;br /&gt;
	end&lt;br /&gt;
		def left&lt;br /&gt;
			puts &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
      	module ClassMethods&lt;br /&gt;
		def right&lt;br /&gt;
			puts &amp;quot;class method - right&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
           end&lt;br /&gt;
class Car&lt;br /&gt;
  	include Actions&lt;br /&gt;
end&lt;br /&gt;
Car.right 			# &amp;quot;class method - right&amp;quot;&lt;br /&gt;
Car.new.left 			# &amp;quot;instance method - left&amp;quot;&lt;br /&gt;
Car.left 			# NoMethodError &lt;br /&gt;
Car.new.right 			# NoMethodError&lt;br /&gt;
&lt;br /&gt;
In the above example, the hook of include is used to modify the class that is including the module. The class is modified to extend the methods of Actions::ClassMethods module. As a result the class Car has access to both the class as well as instance methods.&lt;br /&gt;
&lt;br /&gt;
So, in this manner various techniques and properties of Ruby like singleton classes, method aliasing, class definitions and modules help in implementing meta-programming feature efficiently.&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=53086</id>
		<title>CSC/ECE 517 Fall 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=53086"/>
		<updated>2011-10-20T17:02:32Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Link title]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a cs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ri]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b tj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c cm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c sj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c ka]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d sr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e vs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a sc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e an]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e lm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g jn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i zf]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g rn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h hs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b ns]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b jp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a av]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f jm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ad]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e kt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e gp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b qu]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c bs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2c rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a ca]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b rv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f vh]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3a oe]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h rr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i sd]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d mt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ls]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ch]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4c ap]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4h sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4e cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4a ga]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f sl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4c dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4g as]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4g nv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4j fw]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[trial]]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50985</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50985"/>
		<updated>2011-09-25T23:55:05Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf William R. Cook, Interfaces and Specifications for the Smalltalk-80 Collection Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators.&lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&amp;lt;ref&amp;gt;[http://www.javaworld.com/javaworld/jw-01-1999/jw-01-jglvscoll.html?page=2 Laurence Vanhelsuw, The battle of container frameworks]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0. So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vectors in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1) and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf Sharad Ballepu,Collections – What Happens Within]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29 Maps in C++]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29 Sets implementation in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html Sets implementation in Java]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf Sean Wentworth, David Langan, Thomas Hain,An Empirical Analysis of the Java Collections Framework Versus the C++ Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming) Templates in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Introduction to Ruby(Programming Language)]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html Sets implementation in Ruby]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays Implementation of Arrays in Ruby]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
&lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby Iterators in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 1: Collections framework API]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 2: Documentation about Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50984</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50984"/>
		<updated>2011-09-25T23:52:41Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf William R. Cook, Interfaces and Specifications for the Smalltalk-80 Collection Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators.&lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0. So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vectors in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1) and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf Sharad Ballepu,Collections – What Happens Within]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29 Maps in C++]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29 Sets implementation in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html Sets implementation in Java]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf Sean Wentworth, David Langan, Thomas Hain,An Empirical Analysis of the Java Collections Framework Versus the C++ Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming) Templates in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Introduction to Ruby(Programming Language)]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html Sets implementation in Ruby]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays Implementation of Arrays in Ruby]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
&lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby Iterators in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 1:Collections framework API]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 2:Documentation about Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50982</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50982"/>
		<updated>2011-09-25T23:50:30Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: /* Iterators */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf William R. Cook, Interfaces and Specifications for the Smalltalk-80 Collection Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators.&lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0. So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vectors in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1) and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf Sharad Ballepu,Collections – What Happens Within]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29 Maps in C++]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29 Sets implementation in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html Sets implementation in Java]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf Sean Wentworth, David Langan, Thomas Hain,An Empirical Analysis of the Java Collections Framework Versus the C++ Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming) Templates in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Introduction to Ruby(Programming Language)]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html Sets implementation in Ruby]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays Implementation of Arrays in Ruby]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
&lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby Iterators in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50981</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50981"/>
		<updated>2011-09-25T23:50:06Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: /* Arrays */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf William R. Cook, Interfaces and Specifications for the Smalltalk-80 Collection Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators.&lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0. So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vectors in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1) and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf Sharad Ballepu,Collections – What Happens Within]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29 Maps in C++]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29 Sets implementation in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html Sets implementation in Java]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf Sean Wentworth, David Langan, Thomas Hain,An Empirical Analysis of the Java Collections Framework Versus the C++ Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming) Templates in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Introduction to Ruby(Programming Language)]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html Sets implementation in Ruby]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays Implementation of Arrays in Ruby]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
&lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50980</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50980"/>
		<updated>2011-09-25T23:49:37Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: /* Sets */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf William R. Cook, Interfaces and Specifications for the Smalltalk-80 Collection Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators.&lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0. So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vectors in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1) and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf Sharad Ballepu,Collections – What Happens Within]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29 Maps in C++]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29 Sets implementation in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html Sets implementation in Java]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf Sean Wentworth, David Langan, Thomas Hain,An Empirical Analysis of the Java Collections Framework Versus the C++ Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming) Templates in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Introduction to Ruby(Programming Language)]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html Sets implementation in Ruby]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50977</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50977"/>
		<updated>2011-09-25T23:49:02Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: /* Advantages of Collections in Ruby over Collections Frameworks in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf William R. Cook, Interfaces and Specifications for the Smalltalk-80 Collection Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators.&lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0. So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vectors in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1) and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf Sharad Ballepu,Collections – What Happens Within]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29 Maps in C++]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29 Sets implementation in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html Sets implementation in Java]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf Sean Wentworth, David Langan, Thomas Hain,An Empirical Analysis of the Java Collections Framework Versus the C++ Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming) Templates in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Introduction to Ruby(Programming Language)]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50974</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50974"/>
		<updated>2011-09-25T23:47:50Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: /* Debugging */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf William R. Cook, Interfaces and Specifications for the Smalltalk-80 Collection Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators.&lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0. So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vectors in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1) and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf Sharad Ballepu,Collections – What Happens Within]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29 Maps in C++]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29 Sets implementation in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html Sets implementation in Java]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf Sean Wentworth, David Langan, Thomas Hain,An Empirical Analysis of the Java Collections Framework Versus the C++ Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming) Templates in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50973</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50973"/>
		<updated>2011-09-25T23:47:39Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: /* Readability */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf William R. Cook, Interfaces and Specifications for the Smalltalk-80 Collection Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators.&lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0. So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vectors in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1) and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf Sharad Ballepu,Collections – What Happens Within]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29 Maps in C++]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29 Sets implementation in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html Sets implementation in Java]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf Sean Wentworth, David Langan, Thomas Hain,An Empirical Analysis of the Java Collections Framework Versus the C++ Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming) Templates in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50972</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50972"/>
		<updated>2011-09-25T23:47:14Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: /* Compilation and executable size */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf William R. Cook, Interfaces and Specifications for the Smalltalk-80 Collection Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators.&lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0. So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vectors in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1) and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf Sharad Ballepu,Collections – What Happens Within]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29 Maps in C++]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29 Sets implementation in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html Sets implementation in Java]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf Sean Wentworth, David Langan, Thomas Hain,An Empirical Analysis of the Java Collections Framework Versus the C++ Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming) Templates in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50971</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50971"/>
		<updated>2011-09-25T23:46:21Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: /* Sets */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf William R. Cook, Interfaces and Specifications for the Smalltalk-80 Collection Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators.&lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0. So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vectors in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1) and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf Sharad Ballepu,Collections – What Happens Within]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29 Maps in C++]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29 Sets implementation in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html Sets implementation in Java]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf Sean Wentworth, David Langan, Thomas Hain,An Empirical Analysis of the Java Collections Framework Versus the C++ Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50970</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50970"/>
		<updated>2011-09-25T23:43:24Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: /* Maps */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf William R. Cook, Interfaces and Specifications for the Smalltalk-80 Collection Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators.&lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0. So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vectors in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1) and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf Sharad Ballepu,Collections – What Happens Within]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29 Maps in C++]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50964</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50964"/>
		<updated>2011-09-25T23:40:39Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: /* ArrayList */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf William R. Cook, Interfaces and Specifications for the Smalltalk-80 Collection Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators.&lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0. So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vectors in C++]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1)&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50961</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50961"/>
		<updated>2011-09-25T23:38:31Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: /* History */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf William R. Cook, Interfaces and Specifications for the Smalltalk-80 Collection Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library Standard Template Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators.&lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1)&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50960</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50960"/>
		<updated>2011-09-25T23:35:00Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators. &lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1)&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50959</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50959"/>
		<updated>2011-09-25T23:34:13Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html&lt;br /&gt;
 Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators. &lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1)&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50958</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50958"/>
		<updated>2011-09-25T23:33:59Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html&lt;br /&gt;
 Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators. &lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1)&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50956</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50956"/>
		<updated>2011-09-25T23:33:35Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html&lt;br /&gt;
 Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators. &lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1)&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50953</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50953"/>
		<updated>2011-09-25T23:33:15Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html&lt;br /&gt;
 Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators. &lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1)&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50952</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50952"/>
		<updated>2011-09-25T23:32:55Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html&lt;br /&gt;
 Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators. &lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1)&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50951</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50951"/>
		<updated>2011-09-25T23:32:46Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;ref/&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html&lt;br /&gt;
 Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators. &lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1)&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50950</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50950"/>
		<updated>2011-09-25T23:32:27Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;references/&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html&lt;br /&gt;
 Smalltalk Overview]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators. &lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1)&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50947</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1b_sa&amp;diff=50947"/>
		<updated>2011-09-25T23:30:58Z</updated>

		<summary type="html">&lt;p&gt;Arbukkap: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is about various collection frameworks in programming languages. A brief description of collections and collection framework is discussed and then, the history and evolution of collections is discussed. Later, we discuss how these collections are retrofitted into programming languages. And then, we discuss the advantages of [http://en.wikipedia.org/wiki/Java_collections_framework Collections Framework in Java] over its competing frameworks such as [http://en.wikipedia.org/wiki/Standard_Template_Library STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL]. And then, we showcase some of the positives of having collections being built into the language than having them as class libraries.&lt;br /&gt;
&lt;br /&gt;
== Collection ==&lt;br /&gt;
A collection&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Collection_%28computing%29 All about Collections]&amp;lt;/ref&amp;gt; is a unit that groups multiple data elements into a single unit. These collections are used for storing, retrieving, manipulating and communicating aggregate data. A dictionary (maps words to their meanings), playing cards (collection of cards) can be considered as examples of collections. &lt;br /&gt;
&lt;br /&gt;
== Collection Framework in Programming languages ==&lt;br /&gt;
Collection is a framework that provides interfaces that are well defined for storing and manipulating groups of data as a single unit. These interfaces implement the data structures which are reusable. Though it is a framework, it works as a library. It makes sure that the programmer does not have to create too many interfaces. &lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Collections were first introduced into programming languages, as a part of [http://en.wikipedia.org/wiki/Smalltalk SmallTalk] in the year 1980&amp;lt;ref&amp;gt;[http://web.cecs.pdx.edu/~harry/musings/SmalltalkOverview.html&lt;br /&gt;
]&amp;lt;/ref&amp;gt;. This collection consisted of sets, bags, maps (called as dictionaries), lists with fixed (arrays) and variable size.&amp;lt;ref&amp;gt;[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.7900&amp;amp;rep=rep1&amp;amp;type=pdf]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were later introduced into C++ as Standard Template Library (STL) in the year 1994 by Stepanov and Lee. The collections in this had containers (vectors, maps, lists, stacks, queues and sets), algorithms, iterators, functors.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Standard_Template_Library]&amp;lt;/ref&amp;gt;&lt;br /&gt;
Collections were then introduced into Java initially with the Java 2 platform, Standard Edition, version 1.2, which had synchronized versions of [http://en.wikipedia.org/wiki/Hash_table hash table] and vector. Later, an updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166. This was named as Collections Framework. This framework, which basically works like a library, provides a convenient [http://en.wikipedia.org/wiki/Application_programming_interface Application Programming Interface (API)] to many of the [http://en.wikipedia.org/wiki/Abstract_data_type abstract data types] from computer science [http://en.wikipedia.org/wiki/Data_structure data structure] curriculum: maps, sets, lists, trees, arrays, hash tables and other collections.&lt;br /&gt;
Collections were introduced into Ruby in mid 1990s. These collections were built directly into the programming language. These collections have sets, arrays, hash tables and iterators. &lt;br /&gt;
&lt;br /&gt;
== Retrofitting of Collections into Programming Languages ==&lt;br /&gt;
Now, let us look at how these collections were fitted into the programming languages. A brief description of how various data structures were implemented in programming languages is given below:&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
In this interface, the data need not be in sequence. But we have to note that duplicate elements are not allowed in [http://en.wikipedia.org/wiki/Set_(computer_science) sets]. In many of the languages sets are supported directly. In the rest they can be implemented by a hash table with dummy values. And we use only the keys in the hash table for representing the set. Some of the operations performed on sets are addition, removal or deletion and searching of elements.&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
The next data type is the [http://en.wikipedia.org/wiki/List_(computing) list]. In lists, the order of data or elements of the list is important and we can have duplicate data elements. We have two special types of lists- the queues and the stacks. If addition and deleting of elements in done from either end of the list, then it can be termed as a queue and if the operations are performed at the same end, then they can be termed as stacks. We have to note that, the order of the elements is not changed here. Some of the operations that can be performed on the lists are, adding and deleting data elements present at a particular location in the list, searching for a particular data element in the list and sorting of elements.&lt;br /&gt;
&lt;br /&gt;
=== Bags ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Multiset bag] is similar to set. There is no order of sequence for the data elements in bags. These are usually referred to as multi-sets. The difference between a bag and a set is that, duplicate data elements are allowed in bags. Some of the examples of operations performed on bags are adding and removing of data elements, finding out the count of a particular data element in the bags etc. By performing sorting operation, we can transform a bag into a list.&lt;br /&gt;
&lt;br /&gt;
=== Trees ===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Tree_(data_structure) tree] basically has a root node and some elements or data related to them and those elements and data are further related to more data or elements. The relation among these elements is parent-child relation. So, every element of data has none or more children and every element except the root element, will have a parent element. Examples of operations on trees are the addition of data items so as to maintain a specific property of the tree to perform sorting, etc. and traversals to visit data items in a specific sequence. A tree used for sorting operations is usually called a heap. In most of the frameworks there is direct support for trees and in others, they can be implemented using lists and arrays.  &lt;br /&gt;
&lt;br /&gt;
=== Maps ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Associative_array Maps] are special kind of sets. A map is a set of pairs, each pair representing a one-directional &amp;quot;mapping&amp;quot; from one element to another. These maps can be finite or infinite. Mathematically speaking, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. Some of the operations performed by these Maps include altering, querying, and providing alternative views.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages of Collections in Java vs C++==&lt;br /&gt;
Let us see some of the features that makes Collections framework to rule over other competing frameworks such as [http://www.sgi.com/tech/stl/stl_introduction.html STL] and [http://www.cs.duke.edu/csl/docs/jgl/user/Overview.html#TOP JGL].&lt;br /&gt;
Collections framework is a core Java framework used to provide various data structures and algorithms and an API to access them. STL is a similar framework which is supports C++ and JGL is the decedent of STL in Java. &lt;br /&gt;
&lt;br /&gt;
[[File:collection_archi.gif]]        [[File:jgl_archi.gif]]&lt;br /&gt;
&lt;br /&gt;
===Package Organization===&lt;br /&gt;
&lt;br /&gt;
[[File:package_structure.png]]       [[File:package_stats.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Packaging is one of the important factor that reveals the quality and amount of thought that was put in developing a library.&lt;br /&gt;
The difference in the number of classes shows the major advantage of the Collection framework over the STL/JGL which makes the task simpler with less number of classes to know about.&lt;br /&gt;
&lt;br /&gt;
===Iterator interface hierarchies===&lt;br /&gt;
&lt;br /&gt;
[[File:jgliterator.png]]  [[File:coll_iterator.png]]&lt;br /&gt;
&lt;br /&gt;
Table clearly shows the interface hierarchy in Collections is lot simpler than STL/JGL. The complex approach of STL often leads to developer’s headache due to numerous permutations of incompatible iterators.&lt;br /&gt;
&lt;br /&gt;
=====Range view Selection=====&lt;br /&gt;
Iterators in JGL point to elements. JGL uses these pointers to select a sub range of a container. When we add or remove an element, any iterator pointing to it gets invalidated. So, clearly, JGL doesn't let its iterators remove or add elements because such a capability would sabotage most of its range-based algorithms. More than half of the algorithms doesn’t support the operations on sub range of container instead they work on entire container wasting their time.&lt;br /&gt;
In contrast iterators in Collections point between elements and have an easier approach for selecting sub range in a collection using indices of elements.&lt;br /&gt;
&lt;br /&gt;
===Synchronization===&lt;br /&gt;
In the view of multi threaded environment, all the STL/JGL containers are [http://en.wikipedia.org/wiki/Synchronization_%28computer_science%29 synchronized] which means operations cannot be paralleled and is a certain issue of overhead and performance. Whereas Collections are not synchronized by default but if required provides a [http://en.wikipedia.org/wiki/Thread_safety thread safe] wrapper around the unsynchronized collection.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Collection/Container entities===&lt;br /&gt;
&lt;br /&gt;
====ArrayList====&lt;br /&gt;
&lt;br /&gt;
ArrayList is a resizable form of an Array. Equivalent of ArrayList in STL is vector. When ArrayList constructor ArrayList(int n) is invoked, it creates an empty container and pre allocated space for n elements. So when size method is invoked at this point it returns 0. When a similar constructor of [http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29 Vector] is invoked it creates a container of n elements initialized to 0.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; So when a size method is invoked it returns n. Adding an element at the beginning in ArrayList takes less time than vector because STL’s vector must shift all the n elements before adding a new element.&lt;br /&gt;
C++ vectors does not support in-place reallocation of memory i.e upon reallocation memory held by vector is copied to a new location by the element’s copy constructor and later releasing the memory which is an inefficient process.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:empty.png]]&lt;br /&gt;
&lt;br /&gt;
ArrayList in Collections Framework after default [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 constructor] call&lt;br /&gt;
&lt;br /&gt;
[[File:all_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
vector in STL after default constructor call&lt;br /&gt;
&lt;br /&gt;
[[File:1_filling_stl.png]]&lt;br /&gt;
&lt;br /&gt;
Adding new element 1 by shifting all elements in vector&lt;br /&gt;
&lt;br /&gt;
[[File:1_filled_stl.png]]&lt;br /&gt;
&lt;br /&gt;
After adding 1 in vector&lt;br /&gt;
&lt;br /&gt;
====Maps====&lt;br /&gt;
Maps in Collections framework are internally implemented using arrays. So time taken for accessing, removing a key or checking whether the key is present in the map would be done in the [http://en.wikipedia.org/wiki/Big_O_notation order O(1)] i.e in constant time. Initially maps are initiated to a desired size or default size of 10, so until the resizing of the map it would take constant time i.e O(1)&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; and if size is exceeded it would take linear time in the order of input to resize the map i.e O(n).&amp;lt;ref&amp;gt;[http://www.sharmanj.com/Collections.pdf]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
Maps in STL/JGL are internally implemented using [http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree self-balancing binary search trees] (AVL or Red Black trees). So time taken for accessing, adding, removing a key or checking whether the key is present in the map would be done in the order O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operation !! STL       !! Collections&lt;br /&gt;
|-&lt;br /&gt;
| Searching for an element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
| Inserting a new element || O(log n)  || O(1)**&lt;br /&gt;
|-&lt;br /&gt;
| Incrementing/decrementing an iterator || O(log n)  || O(h/n)*&lt;br /&gt;
|-&lt;br /&gt;
| Removing a single map element || O(log n)  || O(1)&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| Copying an entire map || O(n)  || O(n)&lt;br /&gt;
|-&lt;br /&gt;
| Iterating through all elements || O(n)  || O(n)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 * Where h is table size.&lt;br /&gt;
 ** It takes O(n) when map size need to be resized.&lt;br /&gt;
&lt;br /&gt;
====Sets====&lt;br /&gt;
Sets are internally represented using binary search trees in STL so the operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(log n).&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Set_%28computer_science%29]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets in CF are implemented as [http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html HashSet], [http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html TreeSet], [http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html SortedSet] through maps, binary search trees. HashSet implementation of set can perform set operations such as adding, removing an element, checking whether an element is there or not can be performed in the order of O(1).&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/collections/implementations/set.html]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set operations such as set union, difference and intersection in STL takes more space and time than in Collections framework. Because when these operations are performed on two sets, result is stored in the first set in CF whereas a new set is created and stored in it.&amp;lt;ref&amp;gt;[http://www.cis.usouthal.edu/~hain/general/Publications/oopsla-paper6.pdf]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 // Create the sets&lt;br /&gt;
 Set set1 = new HashSet();&lt;br /&gt;
 Set set2 = new HashSet();&lt;br /&gt;
 // Add elements to the sets ...&lt;br /&gt;
 // Copy all the elements from set2 to set1 (set1 += set2)&lt;br /&gt;
 // set1 becomes the union of set1 and set2&lt;br /&gt;
 set1.addAll(set2);&lt;br /&gt;
 // Remove all the elements in set1 from set2 (set1 -= set2)&lt;br /&gt;
 // set1 becomes the asymmetric difference of set1 and set2&lt;br /&gt;
 set1.removeAll(set2);&lt;br /&gt;
 // Get the intersection of set1 and set2&lt;br /&gt;
 // set1 becomes the intersection of set1 and set2&lt;br /&gt;
 set1.retainAll(set2);&lt;br /&gt;
 // Remove all elements from a set&lt;br /&gt;
 set1.clear();&lt;br /&gt;
&lt;br /&gt;
===Compilation and executable size===&lt;br /&gt;
STL is majorly dependent on templates. As compilers generate additional amount of code for each template type, indiscriminate usage of templates leads to [http://en.wikipedia.org/wiki/Code_bloat code bloat] which results in generation of large executable files.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Readability===&lt;br /&gt;
Code tends to lose its readability when there is a need of lot of new classes to perform a task and if this task involves templates it even makes the code complex and virtually unmaintainable.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt;&lt;br /&gt;
In contrast readability in Java using Collections framework is far better as it performs the task with a minimal set of classes and interfaces.&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
STL has far less support for debugging when compared to Java’s Collection framework. The error messages generated by the template code are far from being readable; one has to get acquainted with these to understand them.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Template_(programming)]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===It's either in the core, or it isn't===&lt;br /&gt;
One of the big advantages of Collections framework over its competing frameworks like STL, JGL which would dwarf any advantage is it is a product of Sun and has been given core status. So any new user of one of these frameworks would certainly go for the one which has “brand” tag attached to it.&lt;br /&gt;
&lt;br /&gt;
== Advantages of Collections in Ruby over Collections Frameworks in Java == &lt;br /&gt;
This column basically discusses about the advantages of having collections built into the languages, like in Ruby&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Ruby_%28programming_language%29]&amp;lt;/ref&amp;gt;, over languages where collections are used as [http://en.wikipedia.org/wiki/Library_(computing) class libraries], like [http://en.wikipedia.org/wiki/Java_collections_framework Java Collections Framework] in Java. &lt;br /&gt;
&lt;br /&gt;
The advantage of having collections built into programming language like in Ruby over Java, where the collections are present as class libraries is that, we can write the equivalent code in fewer lines. This will lead to further advantages, both in terms of speed of development and in terms of bug-fixing.&lt;br /&gt;
An example of this is eliminating duplicates in a list, which is discussed below.&lt;br /&gt;
&lt;br /&gt;
Since the collections are inbuilt in Ruby, all the methods of class libraries can be directly used as operators with the objects. Whereas in Java, where collections are defined in class libraries, we need to create an instance of the collections we want to use in our class and call the methods defined in the collections class to perform any operation related to that collection.&lt;br /&gt;
So, for example, if we want to find unique elements in an array, in Ruby, we can directly use an operator ‘uniq’ as collections are built into the language. In Java, we can achieve this task either by checking through the array manually or we can create an instance of Set and use it to find the unique elements from an arraylist.  &lt;br /&gt;
&lt;br /&gt;
  In Ruby:&lt;br /&gt;
  a = [&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  a.uniq   #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  In Java:  &lt;br /&gt;
  public static &amp;lt;T&amp;gt; void removeDuplicates(ArrayList&amp;lt;T&amp;gt; list) {&lt;br /&gt;
  int size = list.size(); &lt;br /&gt;
  int out = 0;&lt;br /&gt;
  final Set&amp;lt;T&amp;gt; encountered = new HashSet&amp;lt;T&amp;gt;();&lt;br /&gt;
  for (int in = 0; in &amp;lt; size; in++) {&lt;br /&gt;
           final T t = list.get(in);&lt;br /&gt;
           final boolean first = encountered.add(t);&lt;br /&gt;
           if (first) {&lt;br /&gt;
               list.set(out++, t);&lt;br /&gt;
           }&lt;br /&gt;
      }&lt;br /&gt;
      while (out &amp;lt; size) {&lt;br /&gt;
           list.remove(--size);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Speaking in terms of a programmer’s knowledge, Ruby’s inbuilt collections feature is advantageous when compared to Java. For instance, in Java, to achieve a particular functionality, depending on the functionality, sometimes, we need to create instances of other class objects. In that case, we need to understand the functionality of those classes. But in case of Ruby, all the operations and methods are inbuilt, so they can be directly used as operators of objects. For example, in Java, if we want to sort elements in an array, the programmer needs to have idea about the class which is to be implemented here i.e., the ArrayList, since arrays in Java are not sorted and it has to be done by calling another class(ArrayList) where this method is implemented. In case of Ruby, since the collections are inbuilt, the arrays can be sorted just by calling the method ‘sort’.&lt;br /&gt;
   &lt;br /&gt;
   a = [&amp;quot;d&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;]&lt;br /&gt;
   a.sort                    #=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]&lt;br /&gt;
   a.sort {|x, y| y &amp;lt;=&amp;gt; x}   #=&amp;gt; [&amp;quot;e&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One more advantage of having in-built collections in a programming language like Ruby is that it handles the dependency problem. Because, if we have a situation where the software depended on library X, and library X depended on library Y and your software also depended on library Y, but then, it needs a different version of Y, then in such a scenario, Ruby is smart enough to keep those dependencies separate.&lt;br /&gt;
&lt;br /&gt;
Another advantage with Ruby in terms of having in-built collections is that, we get better checking and [http://en.wikipedia.org/wiki/Code_refactoring refactoring] support, than what we get from static types and compilation.&lt;br /&gt;
&lt;br /&gt;
=== Implementation of Collection ===&lt;br /&gt;
Here, we are going to compare the implementation of the following collection entities in Ruby with their implementation in Java.&lt;br /&gt;
&lt;br /&gt;
==== Sets ====&lt;br /&gt;
The advantage of Sets (which is a collection) implemented in Ruby over sets used in Java is that, in Ruby, we can add any type of data element to the set&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html]&amp;lt;/ref&amp;gt;. Whereas in Java, we need to add only data elements of the type defined while defining the set. Let us look at the following example: &lt;br /&gt;
&lt;br /&gt;
  In Ruby: &lt;br /&gt;
  require 'set'&lt;br /&gt;
  s1 = Set.new [1, 2]                   # -&amp;gt; #&amp;lt;Set: {1, 2}&amp;gt;&lt;br /&gt;
  s1.add(&amp;quot;abc&amp;quot;)                         # -&amp;gt; #&amp;lt;Set: {1, 2, &amp;quot;abc&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, from this example we can see that, we can add data elements of different type to a set in Ruby. Now, let us look at the example of a set in Java. &lt;br /&gt;
  &lt;br /&gt;
  In Java:&lt;br /&gt;
  public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Set&amp;lt;String&amp;gt;  someSet = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
           someSet.add(asd);&lt;br /&gt;
           someSet.add(bar);&lt;br /&gt;
    }&lt;br /&gt;
So here, we can observe that, while defining a set, we are also mentioning the type, so here we are creating a set which can hold data elements that are strings. So, if we try to add non-string elements, they will be considered as ineligible elements.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==== Arrays ====&lt;br /&gt;
Having collections like arrays built into the language can be advantageous. For instance, in Ruby, we can increase the size of the array dynamically and automatically&amp;lt;ref&amp;gt;[http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays]&amp;lt;/ref&amp;gt;. For instance, let us consider the following example. Consider an array a = [1, 2, 3];&lt;br /&gt;
So, we can add another element in a[3] without incrementing the size of the array initially. That is,&lt;br /&gt;
a = [1, 2, 3];  	#starting with an array&lt;br /&gt;
a[3] = 4;	#Add a fourth element to it: a is [1, 2, 3, 4].&lt;br /&gt;
This is not the case in Java. In Java, we cannot change the size of an array. An alternative is to instantiate an object of ArrayList from Collections Framework, which can contain only non-primitive elements or objects.&lt;br /&gt;
 &lt;br /&gt;
==== Iterators ====&lt;br /&gt;
Ruby implements [http://en.wikipedia.org/wiki/Iterator iterators] quite differently; all iterations are done by means of passing [http://en.wikipedia.org/wiki/Callback_%28computer_programming%29 callback] closures to container methods - this way, Ruby not only implements basic iteration but also several patterns of iteration like function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method ‘each’. This is because, Ruby has collections built into the language itself.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Iterator#Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
One of the major advantages of collections in Ruby over collections frameworks used in Java is the Iterators. Iterators in Ruby is one of the most known and powerful feature of the language. It works efficiently with blocks and the yield method. We have many variants and options available, the most important fact is that you can pass a block of code to be executed as a callback. No need for interfaces or template methods like in Java.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
[http://www.allapplabs.com/java/java_collection_framework.htm 1:http://www.allapplabs.com/java/java_collection_framework.htm]&lt;br /&gt;
&lt;br /&gt;
[http://java.sun.com/developer/onlineTraining/collections/Collection.html 2:http://java.sun.com/developer/onlineTraining/collections/Collection.html]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/ 3:http://www.ruby-doc.org/]&lt;br /&gt;
&lt;br /&gt;
[http://books.google.com/books?hl=en&amp;amp;lr=&amp;amp;id=jcUbTcr5XWwC&amp;amp;oi=fnd&amp;amp;pg=PR5&amp;amp;dq=ruby+programming&amp;amp;ots=fIBlwi3wcz&amp;amp;sig=a1KD3qk6S6WfX33q6t3e4YWiJYM#v=onepage&amp;amp;q&amp;amp;f=false 3: The Ruby Programming Language, Authors: David Flanagan &amp;amp; Yukihiro Matsumoto]&lt;/div&gt;</summary>
		<author><name>Arbukkap</name></author>
	</entry>
</feed>