<?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=Vvarre</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=Vvarre"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Vvarre"/>
	<updated>2026-07-21T14:31:57Z</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_nv&amp;diff=54534</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54534"/>
		<updated>2011-11-01T03:32:37Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Using method_missing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
&lt;br /&gt;
The other way to create instance specific behavior to the object in the above example is:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 foobar.instance_eval do&lt;br /&gt;
   def size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
 foobar.class # =&amp;gt; Array&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the generic form of creating an instance specific behavior to the newly created object:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class &amp;lt;&amp;lt; obj&lt;br /&gt;
  def hello&lt;br /&gt;
   'Hello World!'&lt;br /&gt;
  end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates a singleton class for the object 'obj' with one instance specific method, 'hello' to it.&lt;br /&gt;
&lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods. &lt;br /&gt;
A meta-class is a class whose instance are classes. Just as an ordinary class defines the behavior of certain objects, a meta-class defines the behavior of certain classes and their instances.We can understand two things from this that the instances of meta-classes are classes and that the meta-class defines the behavior of the class. The instances of the class are created using the class 'Class'(e.g., class.new) in Ruby. All classes are instances of Class. Also any class can have its own behavior by defining singleton methods in Ruby. Hence, the meta-classes are defined for Ruby. Further more, not all classes are singleton classes.. Only singleton classes of classes are meta-classes.&amp;lt;ref&amp;gt;[http://onestepback.org/index.cgi/Tech/Ruby/Metaclasses.red Meta-classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following code returns the Object's singleton class.&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end unless respond_to?(:singleton_class)&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax:&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Dynamic method creation]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://kconrails.com/2010/12/21/dynamic-methods-in-ruby-with-method_missing/ Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 lib/widget.rb&lt;br /&gt;
 class Widget&lt;br /&gt;
  def method_missing sym, *args&lt;br /&gt;
   if sym =~ /^(\w+)=$/&lt;br /&gt;
     instance_variable_set &amp;quot;@#{$1}&amp;quot;, args[0]&lt;br /&gt;
   else&lt;br /&gt;
     instance_variable_get &amp;quot;@#{sym}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 end  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following are used for testing the above example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 ruby-1.9.2-p0 &amp;gt; widget = Widget.new&lt;br /&gt;
  =&amp;gt; #&amp;lt;Widget:0x0000010383f618&amp;gt;&lt;br /&gt;
 ruby-1.9.2-p0 &amp;gt; widget.name = 'Bob'&lt;br /&gt;
  =&amp;gt; &amp;quot;Bob&amp;quot;&lt;br /&gt;
 ruby-1.9.2-p0 &amp;gt; widget.age = 30&lt;br /&gt;
  =&amp;gt; 30&lt;br /&gt;
 ruby-1.9.2-p0 &amp;gt; widget.name&lt;br /&gt;
  =&amp;gt; &amp;quot;Bob&amp;quot;&lt;br /&gt;
 ruby-1.9.2-p0 &amp;gt; widget.age&lt;br /&gt;
  =&amp;gt; 30&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When a object is called on any method, it first checks in its singleton class. Since, it is not found in this class it searches for the method in the original class to which it is referred and then only it searches for 'method_missing' method in the class. In the above example, a Widget object is created that can have any attributes we want to give it. The 'method_missing' checks if the called method ends with an equal sign, '='. If so, then it assigns the value that is passed to an instance variable with that name. If there is no equal sign, then it tries to get the value of instance variable by that name thus illustrating meta-programming concept in Ruby.;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The method with the name 'a_string' is not declared in the class and hence, it invokes the 'method_missing' method declared in the class and thereby dynamically creating thee . One of the ways Ruby is dynamic is that we can always choose to handle those methods that are called but don't actually exist. Ruby handles such an issue using the 'method_missing' method as illustrated above thus illustrating meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54533</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54533"/>
		<updated>2011-11-01T03:32:10Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Using method_missing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
&lt;br /&gt;
The other way to create instance specific behavior to the object in the above example is:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 foobar.instance_eval do&lt;br /&gt;
   def size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
 foobar.class # =&amp;gt; Array&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the generic form of creating an instance specific behavior to the newly created object:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class &amp;lt;&amp;lt; obj&lt;br /&gt;
  def hello&lt;br /&gt;
   'Hello World!'&lt;br /&gt;
  end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates a singleton class for the object 'obj' with one instance specific method, 'hello' to it.&lt;br /&gt;
&lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods. &lt;br /&gt;
A meta-class is a class whose instance are classes. Just as an ordinary class defines the behavior of certain objects, a meta-class defines the behavior of certain classes and their instances.We can understand two things from this that the instances of meta-classes are classes and that the meta-class defines the behavior of the class. The instances of the class are created using the class 'Class'(e.g., class.new) in Ruby. All classes are instances of Class. Also any class can have its own behavior by defining singleton methods in Ruby. Hence, the meta-classes are defined for Ruby. Further more, not all classes are singleton classes.. Only singleton classes of classes are meta-classes.&amp;lt;ref&amp;gt;[http://onestepback.org/index.cgi/Tech/Ruby/Metaclasses.red Meta-classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following code returns the Object's singleton class.&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end unless respond_to?(:singleton_class)&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax:&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Dynamic method creation]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://kconrails.com/2010/12/21/dynamic-methods-in-ruby-with-method_missing/ Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
# lib/widget.rb&lt;br /&gt;
 class Widget&lt;br /&gt;
  def method_missing sym, *args&lt;br /&gt;
   if sym =~ /^(\w+)=$/&lt;br /&gt;
     instance_variable_set &amp;quot;@#{$1}&amp;quot;, args[0]&lt;br /&gt;
   else&lt;br /&gt;
     instance_variable_get &amp;quot;@#{sym}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 end  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following are used for testing the above example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 ruby-1.9.2-p0 &amp;gt; widget = Widget.new&lt;br /&gt;
  =&amp;gt; #&amp;lt;Widget:0x0000010383f618&amp;gt;&lt;br /&gt;
 ruby-1.9.2-p0 &amp;gt; widget.name = 'Bob'&lt;br /&gt;
  =&amp;gt; &amp;quot;Bob&amp;quot;&lt;br /&gt;
 ruby-1.9.2-p0 &amp;gt; widget.age = 30&lt;br /&gt;
  =&amp;gt; 30&lt;br /&gt;
 ruby-1.9.2-p0 &amp;gt; widget.name&lt;br /&gt;
  =&amp;gt; &amp;quot;Bob&amp;quot;&lt;br /&gt;
 ruby-1.9.2-p0 &amp;gt; widget.age&lt;br /&gt;
  =&amp;gt; 30&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When a object is called on any method, it first checks in its singleton class. Since, it is not found in this class it searches for the method in the original class to which it is referred and then only it searches for 'method_missing' method in the class. In the above example, a Widget object is created that can have any attributes we want to give it. The 'method_missing' checks if the called method ends with an equal sign, '='. If so, then it assigns the value that is passed to an instance variable with that name. If there is no equal sign, then it tries to get the value of instance variable by that name thus illustrating meta-programming concept in Ruby.;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The method with the name 'a_string' is not declared in the class and hence, it invokes the 'method_missing' method declared in the class and thereby dynamically creating thee . One of the ways Ruby is dynamic is that we can always choose to handle those methods that are called but don't actually exist. Ruby handles such an issue using the 'method_missing' method as illustrated above thus illustrating meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54532</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54532"/>
		<updated>2011-11-01T02:52:29Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Meta-programming in dynamically typed languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
&lt;br /&gt;
The other way to create instance specific behavior to the object in the above example is:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 foobar.instance_eval do&lt;br /&gt;
   def size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
 foobar.class # =&amp;gt; Array&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the generic form of creating an instance specific behavior to the newly created object:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class &amp;lt;&amp;lt; obj&lt;br /&gt;
  def hello&lt;br /&gt;
   'Hello World!'&lt;br /&gt;
  end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates a singleton class for the object 'obj' with one instance specific method, 'hello' to it.&lt;br /&gt;
&lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods. &lt;br /&gt;
A meta-class is a class whose instance are classes. Just as an ordinary class defines the behavior of certain objects, a meta-class defines the behavior of certain classes and their instances.We can understand two things from this that the instances of meta-classes are classes and that the meta-class defines the behavior of the class. The instances of the class are created using the class 'Class'(e.g., class.new) in Ruby. All classes are instances of Class. Also any class can have its own behavior by defining singleton methods in Ruby. Hence, the meta-classes are defined for Ruby. Further more, not all classes are singleton classes.. Only singleton classes of classes are meta-classes.&amp;lt;ref&amp;gt;[http://onestepback.org/index.cgi/Tech/Ruby/Metaclasses.red Meta-classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following code returns the Object's singleton class.&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end unless respond_to?(:singleton_class)&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax:&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Dynamic method creation]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, when the object is called on any method, it first checks in its singleton class. Since, it is not found in this class it searches for the method in the original class to which it is referred. The method with the name 'anything' is not created and hence, it invokes the 'method_missing' method declared in the class. One of the ways Ruby is dynamic is that we can always choose to handle those methods that are called but don't actually exist. Ruby handles such an issue using the 'method_missing' method as illustrated above thus illustrating meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54450</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54450"/>
		<updated>2011-10-31T16:43:41Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Introducing the Singleton class */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are. Ruby is the best example for the first kind where it is a dynamically typed language.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
&lt;br /&gt;
The other way to create instance specific behavior to the object in the above example is:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 foobar.instance_eval do&lt;br /&gt;
   def size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
 foobar.class # =&amp;gt; Array&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the generic form of creating an instance specific behavior to the newly created object:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class &amp;lt;&amp;lt; obj&lt;br /&gt;
  def hello&lt;br /&gt;
   'Hello World!'&lt;br /&gt;
  end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates a singleton class for the object 'obj' with one instance specific method, 'hello' to it.&lt;br /&gt;
&lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods. &lt;br /&gt;
A meta-class is a class whose instance are classes. Just as an ordinary class defines the behavior of certain objects, a meta-class defines the behavior of certain classes and their instances.We can understand two things from this that the instances of meta-classes are classes and that the meta-class defines the behavior of the class. The instances of the class are created using the class 'Class'(e.g., class.new) in Ruby. All classes are instances of Class. Also any class can have its own behavior by defining singleton methods in Ruby. Hence, the meta-classes are defined for Ruby. Further more, not all classes are singleton classes.. Only singleton classes of classes are meta-classes.&amp;lt;ref&amp;gt;[http://onestepback.org/index.cgi/Tech/Ruby/Metaclasses.red Meta-classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following code returns the Object's singleton class.&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end unless respond_to?(:singleton_class)&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax:&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Dynamic method creation]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, when the object is called on any method, it first checks in its singleton class. Since, it is not found in this class it searches for the method in the original class to which it is referred. The method with the name 'anything' is not created and hence, it invokes the 'method_missing' method declared in the class. One of the ways Ruby is dynamic is that we can always choose to handle those methods that are called but don't actually exist. Ruby handles such an issue using the 'method_missing' method as illustrated above thus illustrating meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54449</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54449"/>
		<updated>2011-10-31T16:42:19Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Introducing the Singleton class */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are. Ruby is the best example for the first kind where it is a dynamically typed language.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
&lt;br /&gt;
The other way to create instance specific behavior to the object in the above example is:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 foobar.insatnce_eval do&lt;br /&gt;
   def size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
 foobar.class # =&amp;gt; Array&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the generic form of creating an instance specific behavior to the newly created object:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class &amp;lt;&amp;lt; obj&lt;br /&gt;
  def hello&lt;br /&gt;
   'Hello World!'&lt;br /&gt;
  end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates a singleton class for the object 'obj' with one instance specific method, 'hello' to it.&lt;br /&gt;
&lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods. &lt;br /&gt;
A meta-class is a class whose instance are classes. Just as an ordinary class defines the behavior of certain objects, a meta-class defines the behavior of certain classes and their instances.We can understand two things from this that the instances of meta-classes are classes and that the meta-class defines the behavior of the class. The instances of the class are created using the class 'Class'(e.g., class.new) in Ruby. All classes are instances of Class. Also any class can have its own behavior by defining singleton methods in Ruby. Hence, the meta-classes are defined for Ruby. Further more, not all classes are singleton classes.. Only singleton classes of classes are meta-classes.&amp;lt;ref&amp;gt;[http://onestepback.org/index.cgi/Tech/Ruby/Metaclasses.red Meta-classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following code returns the Object's singleton class.&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end unless respond_to?(:singleton_class)&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax:&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Dynamic method creation]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, when the object is called on any method, it first checks in its singleton class. Since, it is not found in this class it searches for the method in the original class to which it is referred. The method with the name 'anything' is not created and hence, it invokes the 'method_missing' method declared in the class. One of the ways Ruby is dynamic is that we can always choose to handle those methods that are called but don't actually exist. Ruby handles such an issue using the 'method_missing' method as illustrated above thus illustrating meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54448</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54448"/>
		<updated>2011-10-31T16:37:40Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Introducing the Singleton class */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are. Ruby is the best example for the first kind where it is a dynamically typed language.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
&lt;br /&gt;
The other way to create instance specific behavior to the object in the above example is:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 foobar.insatnce_eval do&lt;br /&gt;
   def size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
 foobar.class # =&amp;gt; Array&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods. &lt;br /&gt;
A meta-class is a class whose instance are classes. Just as an ordinary class defines the behavior of certain objects, a meta-class defines the behavior of certain classes and their instances.We can understand two things from this that the instances of meta-classes are classes and that the meta-class defines the behavior of the class. The instances of the class are created using the class 'Class'(e.g., class.new) in Ruby. All classes are instances of Class. Also any class can have its own behavior by defining singleton methods in Ruby. Hence, the meta-classes are defined for Ruby. Further more, not all classes are singleton classes.. Only singleton classes of classes are meta-classes.&amp;lt;ref&amp;gt;[http://onestepback.org/index.cgi/Tech/Ruby/Metaclasses.red Meta-classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following code returns the Object's singleton class.&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end unless respond_to?(:singleton_class)&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax:&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Dynamic method creation]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, when the object is called on any method, it first checks in its singleton class. Since, it is not found in this class it searches for the method in the original class to which it is referred. The method with the name 'anything' is not created and hence, it invokes the 'method_missing' method declared in the class. One of the ways Ruby is dynamic is that we can always choose to handle those methods that are called but don't actually exist. Ruby handles such an issue using the 'method_missing' method as illustrated above thus illustrating meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54447</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54447"/>
		<updated>2011-10-31T16:32:41Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Meta-programming in dynamically typed languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are. Ruby is the best example for the first kind where it is a dynamically typed language.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
 &lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods. &lt;br /&gt;
A meta-class is a class whose instance are classes. Just as an ordinary class defines the behavior of certain objects, a meta-class defines the behavior of certain classes and their instances.We can understand two things from this that the instances of meta-classes are classes and that the meta-class defines the behavior of the class. The instances of the class are created using the class 'Class'(e.g., class.new) in Ruby. All classes are instances of Class. Also any class can have its own behavior by defining singleton methods in Ruby. Hence, the meta-classes are defined for Ruby. Further more, not all classes are singleton classes.. Only singleton classes of classes are meta-classes.&amp;lt;ref&amp;gt;[http://onestepback.org/index.cgi/Tech/Ruby/Metaclasses.red Meta-classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following code returns the Object's singleton class.&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end unless respond_to?(:singleton_class)&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax:&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Dynamic method creation]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, when the object is called on any method, it first checks in its singleton class. Since, it is not found in this class it searches for the method in the original class to which it is referred. The method with the name 'anything' is not created and hence, it invokes the 'method_missing' method declared in the class. One of the ways Ruby is dynamic is that we can always choose to handle those methods that are called but don't actually exist. Ruby handles such an issue using the 'method_missing' method as illustrated above thus illustrating meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54446</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54446"/>
		<updated>2011-10-31T16:30:19Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Using method_missing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are among which Ruby is the best example.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
 &lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods. &lt;br /&gt;
A meta-class is a class whose instance are classes. Just as an ordinary class defines the behavior of certain objects, a meta-class defines the behavior of certain classes and their instances.We can understand two things from this that the instances of meta-classes are classes and that the meta-class defines the behavior of the class. The instances of the class are created using the class 'Class'(e.g., class.new) in Ruby. All classes are instances of Class. Also any class can have its own behavior by defining singleton methods in Ruby. Hence, the meta-classes are defined for Ruby. Further more, not all classes are singleton classes.. Only singleton classes of classes are meta-classes.&amp;lt;ref&amp;gt;[http://onestepback.org/index.cgi/Tech/Ruby/Metaclasses.red Meta-classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following code returns the Object's singleton class.&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end unless respond_to?(:singleton_class)&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax:&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Dynamic method creation]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, when the object is called on any method, it first checks in its singleton class. Since, it is not found in this class it searches for the method in the original class to which it is referred. The method with the name 'anything' is not created and hence, it invokes the 'method_missing' method declared in the class. One of the ways Ruby is dynamic is that we can always choose to handle those methods that are called but don't actually exist. Ruby handles such an issue using the 'method_missing' method as illustrated above thus illustrating meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54445</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54445"/>
		<updated>2011-10-31T16:26:52Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Using method_missing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are among which Ruby is the best example.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
 &lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods. &lt;br /&gt;
A meta-class is a class whose instance are classes. Just as an ordinary class defines the behavior of certain objects, a meta-class defines the behavior of certain classes and their instances.We can understand two things from this that the instances of meta-classes are classes and that the meta-class defines the behavior of the class. The instances of the class are created using the class 'Class'(e.g., class.new) in Ruby. All classes are instances of Class. Also any class can have its own behavior by defining singleton methods in Ruby. Hence, the meta-classes are defined for Ruby. Further more, not all classes are singleton classes.. Only singleton classes of classes are meta-classes.&amp;lt;ref&amp;gt;[http://onestepback.org/index.cgi/Tech/Ruby/Metaclasses.red Meta-classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following code returns the Object's singleton class.&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end unless respond_to?(:singleton_class)&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax:&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Dynamic method creation]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, since there is no method named anything in the Dummy class, it searches and calls the method_missing in the class and executes. One of the way Ruby is dynamic is that we can always choose to handle those methods that are called but don't actually exist. Ruby handles such an issue using the 'method_missing' method as illustrated above thus illustrating meta-programming in Ruby.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54444</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54444"/>
		<updated>2011-10-31T16:19:51Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Using define_method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are among which Ruby is the best example.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
 &lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods. &lt;br /&gt;
A meta-class is a class whose instance are classes. Just as an ordinary class defines the behavior of certain objects, a meta-class defines the behavior of certain classes and their instances.We can understand two things from this that the instances of meta-classes are classes and that the meta-class defines the behavior of the class. The instances of the class are created using the class 'Class'(e.g., class.new) in Ruby. All classes are instances of Class. Also any class can have its own behavior by defining singleton methods in Ruby. Hence, the meta-classes are defined for Ruby. Further more, not all classes are singleton classes.. Only singleton classes of classes are meta-classes.&amp;lt;ref&amp;gt;[http://onestepback.org/index.cgi/Tech/Ruby/Metaclasses.red Meta-classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following code returns the Object's singleton class.&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end unless respond_to?(:singleton_class)&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax:&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method Dynamic method creation]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, since there is no method named anything in the Dummy class, it searches and calls the method_missing in the class and executes.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54443</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54443"/>
		<updated>2011-10-31T16:18:39Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Introducing the Singleton class */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are among which Ruby is the best example.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
 &lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods. &lt;br /&gt;
A meta-class is a class whose instance are classes. Just as an ordinary class defines the behavior of certain objects, a meta-class defines the behavior of certain classes and their instances.We can understand two things from this that the instances of meta-classes are classes and that the meta-class defines the behavior of the class. The instances of the class are created using the class 'Class'(e.g., class.new) in Ruby. All classes are instances of Class. Also any class can have its own behavior by defining singleton methods in Ruby. Hence, the meta-classes are defined for Ruby. Further more, not all classes are singleton classes.. Only singleton classes of classes are meta-classes.&amp;lt;ref&amp;gt;[http://onestepback.org/index.cgi/Tech/Ruby/Metaclasses.red Meta-classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following code returns the Object's singleton class.&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end unless respond_to?(:singleton_class)&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax:&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, since there is no method named anything in the Dummy class, it searches and calls the method_missing in the class and executes.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54442</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54442"/>
		<updated>2011-10-31T16:16:35Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Introducing the Singleton class */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are among which Ruby is the best example.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
 &lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods. &lt;br /&gt;
A meta-class is a class whose instance are classes. Just as an ordinary class defines the behavior of certain objects, a meta-class defines the behavior of certain classes and their instances.We can understand two things from this that the instances of meta-classes are classes and that the meta-class defines the behavior of the class. The instances of the class are created using the class 'Class'(e.g., class.new) in Ruby. All classes are instances of Class. Also any class can have its own behavior by defining singleton methods in Ruby. Hence, the meta-classes are defined for Ruby. Further more, not all classes are singleton classes.. Only singleton classes of classes are meta-classes.&lt;br /&gt;
&lt;br /&gt;
The following code returns the Object's singleton class.&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end unless respond_to?(:singleton_class)&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax:&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, since there is no method named anything in the Dummy class, it searches and calls the method_missing in the class and executes.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54441</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54441"/>
		<updated>2011-10-31T16:15:38Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Introducing the Singleton class */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are among which Ruby is the best example.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
 &lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods. &lt;br /&gt;
A meta-class is a class whose instance are classes. Just as an ordinary class defines the behavior of certain objects, a meta-class defines the behavior of certain classes and their instances.We can understand two things from this that the instances of meta-classes are classes and that the meta-class defines the behavior of the class. The instances of the class are created using the class 'Class'(e.g., class.new) in Ruby. All classes are instances of Class. Also any class can have its own behavior by defining singleton methods in Ruby. Hence, the meta-classes are defined for Ruby. Further more, not all classes are singleton classes.. Only singleton classes of classes are meta-classes.&lt;br /&gt;
The following code returns the Object's singleton class.&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax:&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, since there is no method named anything in the Dummy class, it searches and calls the method_missing in the class and executes.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54440</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54440"/>
		<updated>2011-10-31T15:50:24Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Using define_method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are among which Ruby is the best example.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
 &lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods.&lt;br /&gt;
The singleton meta-class can be accessed explicitly using the following&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax:&amp;lt;ref&amp;gt;[http://www.raulparolari.com/Ruby2/define_method]&amp;lt;/ref&amp;gt;&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, since there is no method named anything in the Dummy class, it searches and calls the method_missing in the class and executes.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54439</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=54439"/>
		<updated>2011-10-31T15:47:29Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Instance_eval and Class_eval */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are among which Ruby is the best example.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
 &lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods.&lt;br /&gt;
The singleton meta-class can be accessed explicitly using the following&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Using define_method====&lt;br /&gt;
We have seen above how we can create the methods dynamically. One more way of creating the methods dynamically is by using the 'define_method'. This keyword is used to define the methods with the following syntax.&lt;br /&gt;
 define_symbol(symbol,method)&lt;br /&gt;
&amp;lt;p&amp;gt; where symbol specifies the method name; in spite of its name it can be either symbol, a string, or a variable whose value contains the method name and 'method' is a lambda or Proc object containing the method logic.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class C&lt;br /&gt;
   define_method(:hello) do &lt;br /&gt;
     puts &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 C.new.hello #=&amp;gt; &amp;quot;hello from hello method&amp;quot; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code creates an instance method 'hello' and is called using an object of the class and hence the output &amp;quot;hello from hello method&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, since there is no method named anything in the Dummy class, it searches and calls the method_missing in the class and executes.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=53420</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=53420"/>
		<updated>2011-10-20T23:56:46Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Using method_missing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are among which Ruby is the best example.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
 &lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods.&lt;br /&gt;
The singleton meta-class can be accessed explicitly using the following&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless a method called '''method_missing''' is provided. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, since there is no method named anything in the Dummy class, it searches and calls the method_missing in the class and executes.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=53409</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=53409"/>
		<updated>2011-10-20T23:52:23Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Meta-programming in dynamically typed languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are among which Ruby is the best example.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using [http://en.wikipedia.org/wiki/Decorator_pattern Decorators]&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
 &lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods.&lt;br /&gt;
The singleton meta-class can be accessed explicitly using the following&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless you have provided the object with a method called '''method_missing'''. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, since there is no method named anything in the Dummy class, it searches and calls the method_missing in the class and executes.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=53316</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4g nv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4g_nv&amp;diff=53316"/>
		<updated>2011-10-20T22:47:08Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: Created page with &amp;quot;---- ==Introduction== [http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or t...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Meta-programming] in simple terms, is writing of computer programs to write or manipulate other programs (or themselves) as their data, or do that part of the work at compile time that would otherwise be done at runtime. Ruby has open classes which means that, at run time the definition of a class can be changed. All the classes in Ruby are open to be changed by the user at all times. Since, the Ruby class definitions are evaluated at run time, we can enter the world of meta-programming by accomplishing various tasks like compressing the code and so on. It allows how to define methods and classes even during the run-time. &lt;br /&gt;
&lt;br /&gt;
By cleverly planning and applying the techniques of meta-programming, the code can be written efficiently that is DRYer, lighter, more intuitive and more scalable. Few primitive languages that support meta-programming includes Lisp which provides macros as a standard facility to write code that will be directly read and compiled by the system and Prolog which provides simple clause expansions that may be used to generate code as needed. Meta-programming involves thinking about the needs of the system and taking advantage of simpler specifications whenever possible.&lt;br /&gt;
&lt;br /&gt;
==Meta-programming in dynamically typed languages==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic programming languages] are a class of high-level programming languages that are executed at runtime with many common behaviors that other languages might perform during compilation, if at all. These behaviors could include program extension, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. The common examples of dynamic languages are PHP, JavaScript, Perl, Ruby and Python.  Most dynamic languages are dynamically typed, but not all are among which Ruby is the best example.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]:  Ruby is an interpreted scripting language for quick and easy object oriented programming. It is a dynamic, reflective language that has syntax similar to Perl and Python.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class A; end&lt;br /&gt;
    A.class_eval do&lt;br /&gt;
     def foo;&lt;br /&gt;
       puts 'foo';&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   &lt;br /&gt;
   A.foo       # =&amp;gt; Gives the error saying NoMethodError&lt;br /&gt;
   A.new.foo   # =&amp;gt; Gives the output as &amp;quot;foo&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]: Python is an interpreted, object-oriented language that combined with dynamic typing and dynamic binding. &lt;br /&gt;
Metaprogramming in python is supported using Decorators&amp;lt;ref&amp;gt; [http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/ Meta-programming in Python]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Class Ninja(object):&lt;br /&gt;
    def  __init__(self,name) :&lt;br /&gt;
     self.name = name&lt;br /&gt;
&lt;br /&gt;
     drew = Ninja(‘drew’) &lt;br /&gt;
     adam = Ninja(‘adam’)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Adding a method to the class:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   def battle_cry(self)&lt;br /&gt;
     print  ‘%s says zing!!’ % self.name&lt;br /&gt;
   Ninja.battle_cry = battle_cry&lt;br /&gt;
 &lt;br /&gt;
   Drew.battle_cry()  # =&amp;gt; Drew says zing!!&lt;br /&gt;
   Adam.battle_cry() # =&amp;gt; Adam says zing!!&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Adding a method to an instance:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   import types&lt;br /&gt;
   def throw_star(self)&lt;br /&gt;
     print ‘throwing a star’&lt;br /&gt;
   drew.throw_star = types.MethodType(throw_star,drew)&lt;br /&gt;
 &lt;br /&gt;
   drew.throw_star()  # =&amp;gt; throwing a star&lt;br /&gt;
   drew.__getattribute__(‘battle_cry’)()  # =&amp;gt; Drew says zing!!  {Invoking the method dynamically}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The decorators enhance the original function by adding the functionality of element wise. Now, we can operate on one thing or a series of things which can be applicable efficiently in many dynamic programming situations.&lt;br /&gt;
&lt;br /&gt;
==Ways to implement Meta-programming==&lt;br /&gt;
=== Introducing the Singleton class ===&lt;br /&gt;
 &lt;br /&gt;
The basic fundamental feature of metaprogramming is Singleton. The [http://www.wikyblog.com/AmanKing/Singleton%20class%20in%20Ruby Singleton class] is the class created for an object if the Ruby interpreter needs to add an instance-specific behavior for that object. All objects in Ruby are open to modification and any particular instance of any class can be changed. Ruby allows us to add a new method to a single object. If there is only one single object that requires addition, alteration or deletion, introducing singleton class is the best way to accomplish this objective. A singleton class is a class whose number of instances that can be instantiated is limited to one. The singleton for class objects is called as [http://en.wikipedia.org/wiki/Metaclass Meta-class].&lt;br /&gt;
 &lt;br /&gt;
With access to a class object’s meta-class we can then use meta-programming techniques of Ruby to enhance the class&amp;lt;ref&amp;gt;[http://www.devalot.com/articles/2008/09/ruby-singleton Concept of Singleton class]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us look at the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  foobar = Array.new&lt;br /&gt;
   def foobar.size&lt;br /&gt;
    &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
  foobar.size  # =&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
  foobar.class # =&amp;gt; Array&lt;br /&gt;
 &lt;br /&gt;
  bizbat = Array.new&lt;br /&gt;
  bizbat.size  # =&amp;gt; 0&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In the above example, a new object of Array is created and we are adding methods to the object. This way only the object can access the modified method size unlike the other objects of the same class that have access to the method of the original class. This way any number of methods can be added with the singleton class.&lt;br /&gt;
 &lt;br /&gt;
In Ruby, the Singleton class itself is treated as a receiver. When any method is added to a specific object, Ruby inserts a new anonymous class called a Singleton class into the hierarchy as a container to hold these types of methods.&lt;br /&gt;
The singleton meta-class can be accessed explicitly using the following&amp;lt;ref&amp;gt;[http://www.slideshare.net/bsbodden/ruby-metaprogramming-08 Singleton Class]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module Kernel&lt;br /&gt;
  def singleton_class&lt;br /&gt;
   class &amp;lt;&amp;lt; self&lt;br /&gt;
    self&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 end &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Method Aliasing===&lt;br /&gt;
&lt;br /&gt;
Aliasing means giving a second name to a method or variable. '''Method Aliasing''' is primarily used to override methods and change behavior of classes and objects in order to provide more expressive options to programmers. Ruby provides this functionality with the alias and alias_method keywords. The alias keyword takes two arguments: the old method/variable name and the new method/variable name and create a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&amp;amp;, $`, $', and $+).  The method names should be passed as symbols which are immutable as opposed to mutable strings. Aliasing cannot be effectively applied on local variables ,instance variables and constants&amp;lt;ref&amp;gt;[http://ruby.about.com/od/rubyfeatures/a/aliasing.htm Method aliasing]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
General syntax for aliasing is:-&lt;br /&gt;
 alias_attribute(new_name, old_name)&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Objectcount&lt;br /&gt;
  attr_accessor :count&lt;br /&gt;
  alias :number :count   # alias for getter&lt;br /&gt;
  def initialize(cnt)&lt;br /&gt;
   @count = cnt&lt;br /&gt;
  end  &lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 a = Objectcount.new(5)&lt;br /&gt;
 puts a.number&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
In the following example, start is defined as alias of method begun, and hence object of the class Race can invoke begun method using r.start.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Race&lt;br /&gt;
   def begun&lt;br /&gt;
     puts &amp;quot;Race Started&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   alias :start :begun&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 r = Race.new&lt;br /&gt;
 r.start # should refer to r.begun&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. Aliases and methods to individual objects can also be addes using a syntax similar to the inherited class syntax.  Following example illustrate a way to override the behavior of some method without changing its original implementation. In the following example, a Tax class is declared and an instance is created. The class declaration uses the alias to alter the behavior of payment method to display the payment type from monthly to yearly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tax&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Monthly Bill&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  alias_method :expenditure, :payment&lt;br /&gt;
  def payment&lt;br /&gt;
   puts &amp;quot;Yearly Bill &amp;quot;&lt;br /&gt;
   expenditure&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 tx = Tax.new&lt;br /&gt;
 tx.payment&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  Output:&lt;br /&gt;
   &amp;quot; Yearly Bill &amp;quot;&lt;br /&gt;
   &amp;quot; Monthly Bill &amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Problem with Aliasing'''&lt;br /&gt;
*Aliases encourage Monkeypatching and thus hold the possiblity of breaking the existing code. Look back at the last section. Adding a alias for String#length breaks libraries that expect the “length” of a string to be its size in bytes. Following can provide a workaround for such issues :-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class String&lt;br /&gt;
  alias :real_length :length&lt;br /&gt;
  def length&lt;br /&gt;
   real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
 &amp;quot;Hello World&amp;quot;.real_length # =&amp;gt; 11&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Loading an Alias twice also disrupt the behaviour and throws exceptions.&lt;br /&gt;
&lt;br /&gt;
===Class Definitions===&lt;br /&gt;
In languages such as C++ and Java, class definitions are processed at compile time which require compiler to load symbol tables, work out storage allocation and constructs dispatch tables. Whereas, in Ruby, class and module definitions are executable code which are parsed at compile time, but created at runtime when they are encountered. This flexiblity allow users to structure programs dynamically. Classes in Ruby are not closed and can thus be extended to add new methods at runtime. Even core system classes like String, Array, and Fixum can be extended just by reopening the class and adding  new code&amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ Classes and Methods]&amp;lt;/ref&amp;gt;. For example :&lt;br /&gt;
&lt;br /&gt;
Redefine existing method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  class String&lt;br /&gt;
   def length&lt;br /&gt;
    real_length &amp;gt; 5 ? 'long' : 'short'&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  &amp;quot;Hello World&amp;quot;.length # =&amp;gt; &amp;quot;long&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add new method:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 # reopen the class&lt;br /&gt;
 String.class_eval do&lt;br /&gt;
  # define new method&lt;br /&gt;
  def get_size&lt;br /&gt;
  # method implementation&lt;br /&gt;
   length&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Ruby class is an object of class Class, which contains objects, list of methods and a reference to a superclass. All method calls in Ruby nominate a receiver (self, the current object). Ruby finds the method to invoke by looking at the list of methods in the receiver's class. If it doesn't find the method there, it looks in the superclass, and then in the superclass's superclass, and so on. If the method cannot be found in the receiver's class or any of its ancestors, Ruby invokes the method method_missing on the original receiver.&lt;br /&gt;
&lt;br /&gt;
====Instance_eval and Class_eval====&lt;br /&gt;
&lt;br /&gt;
There are several versions of evaluation primitives in Ruby. The available contestants are eval, instance_eval, module_eval and class_eval. Firstly, class_eval is an alias for module_eval. Second, there are some differences between eval and the others. Importantly, eval takes only a string to evaluate while the others can evaluate a block instead. The eval involves many complexities and should be the last way to do anything. In most of the cases, we can get away with just evaluating blocks with instance_eval and block_eval.&lt;br /&gt;
 &lt;br /&gt;
In a class (or module) definition, the class itself takes the role of the current object self. When you define a method, that method becomes an instance method of the current class.  Module#class_eval( ) (also known by its alternate name, module_eval( )) evaluates a block in the context of an existing class. instance_eval( ) only changes self, while class_eval( ) changes both self and the current class. By changing the current class, class_eval( ) effectively reopens the class, just like the class keyword does.&lt;br /&gt;
&lt;br /&gt;
Instance_eval will evaluate the string or the block in the context of the receiver which means that self will be set to the receiver while evaluating. class_eval will evaluate the string or the block in the context of the module it is called on. Instance_eval opens an object that is not a class whereas class_eval() opens a class definition and defines methods with def&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Instance_eval and Class_eval]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Consider the example illustrating class_eval:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.class_eval do&lt;br /&gt;
  def say_hello&lt;br /&gt;
   &amp;quot;Hello!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 jimmy = Person.new&lt;br /&gt;
 jimmy.say_hello         #  =&amp;gt;  &amp;quot;Hello!&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Consider the following example illustrating instance_eval:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Person&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.instance_eval do&lt;br /&gt;
  def human?&lt;br /&gt;
   true&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 Person.human?   	# =&amp;gt;  true&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Evaluation Table:&amp;lt;/b&amp;gt;  In the following table, new scope means that code inside the block does not have access to local variables outside of the block. Each object in Ruby has its own metaclass, which is a Class that can have methods, but is only attached to the object itself.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mechanism&lt;br /&gt;
! Method Resolution&lt;br /&gt;
! Method Definition&lt;br /&gt;
! New Scope&lt;br /&gt;
|-&lt;br /&gt;
| Class Person&lt;br /&gt;
| Person &lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Class &amp;lt;&amp;lt; Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| Same&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Person.class_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Same&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Person.instance_eval&lt;br /&gt;
| Person&lt;br /&gt;
| Person's metaclass&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
====Using method_missing====&lt;br /&gt;
 &lt;br /&gt;
When any message is sent to an object, the object executes the first method it finds on its method lookup path with the same name as the message. Method lookup path is nothing but the class in hierarchy to the Object class. If it fails to find any such method, it raises a NoMethodError exception unless you have provided the object with a method called '''method_missing'''. This method is passed with the symbol of the non-existent method, an array of arguments that were passed in the original call and any block passed to the original method&amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/ruby_method_missing.html Method_missing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   class Dummy &lt;br /&gt;
    def method_missing(m, *args, &amp;amp;block)  &lt;br /&gt;
      puts &amp;quot;There's no method called #{m} here -- please try again.&amp;quot; &lt;br /&gt;
    end  &lt;br /&gt;
   end  &lt;br /&gt;
 &lt;br /&gt;
  Dummy.new.anything  &lt;br /&gt;
 &lt;br /&gt;
  The output is:&lt;br /&gt;
     There's no method called anything here -- please try again.   &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the above example, since there is no method named anything in the Dummy class, it searches and calls the method_missing in the class and executes.&lt;br /&gt;
&lt;br /&gt;
====Singleton methods====&lt;br /&gt;
[http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby Singleton method] is a method that is defined only for a single object. Singleton methods helps in customizing specific instance of a class and thus escape from the need of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods&amp;lt;ref&amp;gt; [http://www.rubyist.net/~slagell/ruby/ Singleton Methods] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class SingletonTest&lt;br /&gt;
  def size&lt;br /&gt;
   25&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 test1 = SingletonTest.new&lt;br /&gt;
 test2 = SingletonTest.new&lt;br /&gt;
 &lt;br /&gt;
 def test2.size&lt;br /&gt;
  10&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 test1.size   # =&amp;gt; 25&lt;br /&gt;
 test2.size   # =&amp;gt; 10&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
Modules are a way of grouping methods, classes, and constants. The major benefits using modules is that they provide a namespace and prevent name clashes. They also implement the mixin facility. Ruby allows you to create and modify classes and modules dynamically. Almost any operation can be done on any class or module that isn’t frozen.&lt;br /&gt;
&lt;br /&gt;
Modules have simple access to the method behavior to anyone who wishes to alter but reuse the original behavior. This fact could be applied to both classes that include modules and instances that extend the modules.&lt;br /&gt;
&lt;br /&gt;
'''Include:''' Module.include is generally used to mix modules into classes and other modules. Whenever a module is included the constants, methods and the variables of the modules are added to the including module or class instances. This include statement causes all methods in modules to be added as the instance methods and the constants can also be accessed through the method whereas the extend statement adds all the methods as class methods and does nothing with the constants&amp;lt;ref&amp;gt;[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Extending modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
Consider the following example:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 module Actions&lt;br /&gt;
  def left(steps)&lt;br /&gt;
    Movement.new(self, steps)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 class Car&lt;br /&gt;
  include Actions&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
After Actions is included left can be executed by any instance of Car.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 car = Car.new&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, '''Object.extend'''(module, ..) adds the instance methods from each module given as a parameter. However, extend adds the methods to one instance, not to all instances.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new                                   	&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 movement = car.left&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code gives car the same behavior as include would, except only the instance that called extend would have this new behavior. Therefore, the above code is valid, but this code would result in an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 car = Car.new&lt;br /&gt;
 car.extend Actions&lt;br /&gt;
 car2 = Car.new&lt;br /&gt;
 movement = car2.left  # calling 'left' here is invalid because car2 does not extend Actions.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Extend is commonly used to mix instance methods of a module into a class. Ruby invokes included or extended methods in the module, if they exist, when the module is included or extended respectively. These have to be defined as class methods - even though this is not a class. Both these methods should take a single parameter; the object or class to which the module is being added&amp;lt;ref&amp;gt;[http://strugglingwithruby.blogspot.com/2009/02/modules.html Modules]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
Let us consider the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 module TestModule&lt;br /&gt;
  def self.included base&lt;br /&gt;
    p &amp;quot;I am being included by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def self.extended base&lt;br /&gt;
    p &amp;quot;I am being extended by #{base}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass1&lt;br /&gt;
  include TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 class TestClass2&lt;br /&gt;
  extend TestModule&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 p 'Classes now defined'&lt;br /&gt;
 &lt;br /&gt;
 tc2 = TestClass2.new&lt;br /&gt;
 tc2.extend TestModule&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 &amp;quot;I am being included by TestClass1&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by TestClass2&amp;quot;&lt;br /&gt;
 &amp;quot;Classes now defined&amp;quot;&lt;br /&gt;
 &amp;quot;I am being extended by #&amp;lt;TestClass2:0x987a33&amp;gt;”&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Other ways to implement Meta-programming===&lt;br /&gt;
*'''Introspect on instance variables:''' A trick that Rails uses to make instance variables from the controller available in the view is to introspect on an objects instance variables. It's easy to do with instance_variables, using instance_variable_get and instance_variable_set. We could copy all the instance variables from one object to another using to accomplish this property.&lt;br /&gt;
*'''Create Procs from blocks and send them around:''' Materializing a Proc and saving this in variables and sending it around makes many API's very easy to use. This is one of the ways markably used to manage those CSS class definitions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Meta-programming==&lt;br /&gt;
*It is a new set of conceptual tools that eliminates duplication form your code.&lt;br /&gt;
*It allows us to create more expressive APIs than without it and can accomplish some things with      significantly less code.&lt;br /&gt;
*Ruby can enter into the field of Artificial Intelligence.&lt;br /&gt;
*In Real-time, as the analysis is done at runtime any changes to the data model can be reflected in the application at real-time.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
Ruby’s way to enhance a class using metaprogramming  is seamless, powerful, and built right into the language itself. Metaprogramming helps in keeping code simple and precise.  For the most part, the only code that exists in your classes is custom methods to handle specific business rules and other specific one-off customizations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*http://rosettacode.org/wiki/Metaprogramming&lt;br /&gt;
*http://rubysource.com/ruby-metaprogramming-part-i/&lt;br /&gt;
*http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html&lt;br /&gt;
*http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=52854</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=52854"/>
		<updated>2011-10-20T02:43:57Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: &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 nv]]&lt;br /&gt;
&lt;br /&gt;
*[[trial]]&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=51169</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=51169"/>
		<updated>2011-09-26T03:10:28Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages whose structures are modularized into sections of code enclosed between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure&amp;lt;ref&amp;gt;[http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview ]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm Structured Programming] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers learning Object Oriented Programming] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Compare and contrast structured and object oriented]&amp;lt;/ref&amp;gt; &amp;lt;ref&amp;gt;[http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs. Object oriented Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks.&amp;lt;ref&amp;gt;[http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure necessary]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
* http://www.cplusplus.com/doc/tutorial/control/ C++&lt;br /&gt;
* http://www.cs.rpi.edu/~szymansk/OOF90/F90_Objects.html&lt;br /&gt;
* http://www.ibiblio.org/g2swap/byteofpython/read/oops.html&lt;br /&gt;
* http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview&lt;br /&gt;
* http://www.triconsole.com/php/oop.php&lt;br /&gt;
* http://ascelibrary.org/cpo/resource/1/jccee5/v18/i3/p226_s1 Object-Oriented Paradigm in Programming for Computer-Aided Analysis of Structures&lt;br /&gt;
* http://expressionflow.com/category/object-oriented-programming/intro-to-oop-in-labview/&lt;br /&gt;
* http://dl.acm.org/citation.cfm?id=36164 Block Structure and Object Oriented Languages&lt;br /&gt;
* http://en.wikipedia.org/wiki/Smalltalk Smalltalk&lt;br /&gt;
* http://www.php-developer.org/learn-php-oop-object-oriented-programming-for-beginners-tutorial/ PHP&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=51168</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=51168"/>
		<updated>2011-09-26T03:09:59Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages whose structures are modularized into sections of code enclosed between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure&amp;lt;ref&amp;gt;[http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview ]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm Structured Programming] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers learning Object Oriented Programming] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Compare and contrast structured and object oriented]&amp;lt;/ref&amp;gt; &amp;lt;ref&amp;gt;[http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs. Object oriented Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks.&amp;lt;ref&amp;gt;[http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure necessary]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
* http://www.cplusplus.com/doc/tutorial/control/ C++&lt;br /&gt;
* http://www.cs.rpi.edu/~szymansk/OOF90/F90_Objects.html&lt;br /&gt;
* http://www.ibiblio.org/g2swap/byteofpython/read/oops.html&lt;br /&gt;
* http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview&lt;br /&gt;
* http://www.triconsole.com/php/oop.php&lt;br /&gt;
* http://ascelibrary.org/cpo/resource/1/jccee5/v18/i3/p226_s1 Object-Oriented Paradigm in Programming for Computer-Aided Analysis of Structures&lt;br /&gt;
* http://expressionflow.com/category/object-oriented-programming/intro-to-oop-in-labview/&lt;br /&gt;
* http://dl.acm.org/citation.cfm?id=36164 Block Structure and Object Oriented Languages&lt;br /&gt;
* http://en.wikipedia.org/wiki/Smalltalk Smalltalk&lt;br /&gt;
* http://www.php-developer.org/learn-php-oop-object-oriented-programming-for-beginners-tutorial/ PHP&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=51163</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=51163"/>
		<updated>2011-09-26T03:04:11Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages whose structures are modularized into sections of code enclosed between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure&amp;lt;ref&amp;gt;[http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview ]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm Structured Programming] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers learning Object Oriented Programming] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Compare and contrast structured and object oriented]&amp;lt;/ref&amp;gt; &amp;lt;ref&amp;gt;[http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs. Object oriented Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks.&amp;lt;ref&amp;gt;[http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure necessary]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50768</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50768"/>
		<updated>2011-09-25T22:26:11Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages whose structures are modularized into sections of code enclosed between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure&amp;lt;ref&amp;gt;[http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview ]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm Structured Programming] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers learning Object Oriented Programming] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Compare and contrast structured and object oriented]&amp;lt;/ref&amp;gt; &amp;lt;ref&amp;gt;[http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs. Object oriented Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks.&amp;lt;ref&amp;gt;[http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure necessary]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50753</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50753"/>
		<updated>2011-09-25T22:13:43Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Comparision between Block Structured and OO Programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure&amp;lt;ref&amp;gt;[http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview ]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm Structured Programming] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers learning Object Oriented Programming] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Compare and contrast structured and object oriented]&amp;lt;/ref&amp;gt; &amp;lt;ref&amp;gt;[http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs. Object oriented Programming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks.&amp;lt;ref&amp;gt;[http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure necessary]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50750</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50750"/>
		<updated>2011-09-25T22:12:57Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Usage of Block Structures in Object Oriented Programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure&amp;lt;ref&amp;gt;[http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview ]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm Structured Programming] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers learning Object Oriented Programming] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Compare and contrast structured and object oriented]&amp;lt;/ref&amp;gt; &amp;lt;ref&amp;gt;[http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks.&amp;lt;ref&amp;gt;[http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure necessary]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50749</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50749"/>
		<updated>2011-09-25T22:12:25Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Usage of Block Structures in Object Oriented Programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure&amp;lt;ref&amp;gt;[http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview ]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm Structured Programming] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers learning Object Oriented Programming] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Compare and contrast structured and object oriented]&amp;lt;/ref&amp;gt; &amp;lt;ref&amp;gt;[http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks.&amp;lt;ref&amp;gt;[    http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure necessary]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50748</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50748"/>
		<updated>2011-09-25T22:11:41Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Usage of Block Structures in Object Oriented Programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure&amp;lt;ref&amp;gt;[http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview ]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm Structured Programming] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers learning Object Oriented Programming] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Compare and contrast structured and object oriented]&amp;lt;/ref&amp;gt; &amp;lt;ref&amp;gt;[http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks.&amp;lt;ref&amp;gt;[    http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf]&amp;lt;/ref&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50746</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50746"/>
		<updated>2011-09-25T22:08:16Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Comparision between Block Structured and OO Programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure&amp;lt;ref&amp;gt;[http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview ]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm Structured Programming] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers learning Object Oriented Programming] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Compare and contrast structured and object oriented]&amp;lt;/ref&amp;gt; &amp;lt;ref&amp;gt;[http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50736</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50736"/>
		<updated>2011-09-25T22:00:57Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* History */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure&amp;lt;ref&amp;gt;[http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview ]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm Structured Programming] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers learning Object Oriented Programming] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Compare and contrast structured and object oriented]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50735</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50735"/>
		<updated>2011-09-25T21:59:38Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* History */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure&amp;lt;ref&amp;gt;[http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview ]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm Structured Programming] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Compare and contrast structured and object oriented]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50734</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50734"/>
		<updated>2011-09-25T21:58:49Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Comparision between Block Structured and OO Programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure&amp;lt;ref&amp;gt;[http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview ]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Compare and contrast structured and object oriented]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50732</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50732"/>
		<updated>2011-09-25T21:57:10Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* What is Object-Oriented Programming? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure&amp;lt;ref&amp;gt;[http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview ]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50728</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50728"/>
		<updated>2011-09-25T21:55:45Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* What is Object-Oriented Programming? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure&amp;lt;ref&amp;gt;[http://www.delphibasics.co.uk/Article.asp?Name=OO ]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50724</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50724"/>
		<updated>2011-09-25T21:53:12Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Comparision between Block Structured and OO Programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&amp;lt;ref&amp;gt;[http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50716</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50716"/>
		<updated>2011-09-25T21:49:44Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* History */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;ref&amp;gt;[http://bulletin.sigchi.org/1997/october/papers/ross/] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50714</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50714"/>
		<updated>2011-09-25T21:49:13Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* History */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming &amp;lt;/ref&amp;gt; [http://bulletin.sigchi.org/1997/october/papers/ross/] &amp;lt;/ref&amp;gt; was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50710</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50710"/>
		<updated>2011-09-25T21:47:05Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* History */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming &amp;lt;ref&amp;gt; [http://www.wisegeek.com/what-is-structured-programming.htm] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50706</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50706"/>
		<updated>2011-09-25T21:43:30Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50705</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50705"/>
		<updated>2011-09-25T21:42:54Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* What is a Block-Structured Programming? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming &amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Block_(programming) Block (programming)] &amp;lt;/ref&amp;gt; languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Block_(programming) Block Programming&lt;br /&gt;
* http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers Learning Object-Oriented Programming&lt;br /&gt;
* http://www.cplusplus.com/doc/tutorial/control/ C++&lt;br /&gt;
* http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Contrast Structured and OOP&lt;br /&gt;
* http://www.cs.rpi.edu/~szymansk/OOF90/F90_Objects.html&lt;br /&gt;
* http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs Object Oriented Programming&lt;br /&gt;
* http://www.ibiblio.org/g2swap/byteofpython/read/oops.html&lt;br /&gt;
* http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview&lt;br /&gt;
* http://www.triconsole.com/php/oop.php&lt;br /&gt;
* http://www.wisegeek.com/what-is-structured-programming.htm What is Structured Programming&lt;br /&gt;
* http://ascelibrary.org/cpo/resource/1/jccee5/v18/i3/p226_s1 Object-Oriented Paradigm in Programming for Computer-Aided Analysis of Structures&lt;br /&gt;
* http://expressionflow.com/category/object-oriented-programming/intro-to-oop-in-labview/&lt;br /&gt;
* http://dl.acm.org/citation.cfm?id=36164 Block Structure and Object Oriented Languages&lt;br /&gt;
* http://en.wikipedia.org/wiki/Smalltalk Smalltalk&lt;br /&gt;
* http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure Necessary?&lt;br /&gt;
* http://www.php-developer.org/learn-php-oop-object-oriented-programming-for-beginners-tutorial/ PHP&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50694</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50694"/>
		<updated>2011-09-25T21:29:52Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* What is Object-Oriented Programming? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language where there are two classes, Tree and PlantTrees and three objects are created to find the heights for each of the three objects.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class Tree &lt;br /&gt;
 { &lt;br /&gt;
   public int height = 0;&lt;br /&gt;
   public void grow() &lt;br /&gt;
   { &lt;br /&gt;
    height = height + 1; &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class PlantTrees &lt;br /&gt;
 { &lt;br /&gt;
   public static void main(String[] args) &lt;br /&gt;
   {  &lt;br /&gt;
    System.out.println(&amp;quot;Let's plant some trees!&amp;quot;); &lt;br /&gt;
    Tree tree1 = new Tree(); &lt;br /&gt;
    System.out.println(&amp;quot;I've created a tree with a height of &amp;quot; + tree1.height + &amp;quot; meter(s).&amp;quot;);  &lt;br /&gt;
    tree1.grow(); &lt;br /&gt;
    System.out.println(&amp;quot;After a bit of growth, it is now up to &amp;quot; + tree1.height + &amp;quot; meter(s) tall.&amp;quot;); &lt;br /&gt;
    Tree tree2 = new Tree();  &lt;br /&gt;
    Tree tree3 = new Tree();&lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    tree3.grow();  &lt;br /&gt;
    tree2.grow();  &lt;br /&gt;
    tree3.grow(); &lt;br /&gt;
    tree2.grow();&lt;br /&gt;
    System.out.println(&amp;quot;Here are the final heights:&amp;quot;);     &lt;br /&gt;
    System.out.println(&amp;quot; tree1: &amp;quot; + tree1.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree2: &amp;quot; + tree2.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
    System.out.println(&amp;quot; tree3: &amp;quot; + tree3.height + &amp;quot;m&amp;quot;);   &lt;br /&gt;
   }&lt;br /&gt;
 }  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Let's plant some trees! &lt;br /&gt;
 I've created a tree with a height of 0 meter(s).  &lt;br /&gt;
 After a bit of growth, it is now up to 1 meter(s) tall. &lt;br /&gt;
 Here are the final heights: &lt;br /&gt;
 tree1: 1m     &lt;br /&gt;
 tree2: 3m   &lt;br /&gt;
 tree3: 2m  &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Block_(programming) Block Programming&lt;br /&gt;
* http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers Learning Object-Oriented Programming&lt;br /&gt;
* http://www.cplusplus.com/doc/tutorial/control/ C++&lt;br /&gt;
* http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Contrast Structured and OOP&lt;br /&gt;
* http://www.cs.rpi.edu/~szymansk/OOF90/F90_Objects.html&lt;br /&gt;
* http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs Object Oriented Programming&lt;br /&gt;
* http://www.ibiblio.org/g2swap/byteofpython/read/oops.html&lt;br /&gt;
* http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview&lt;br /&gt;
* http://www.triconsole.com/php/oop.php&lt;br /&gt;
* http://www.wisegeek.com/what-is-structured-programming.htm What is Structured Programming&lt;br /&gt;
* http://ascelibrary.org/cpo/resource/1/jccee5/v18/i3/p226_s1 Object-Oriented Paradigm in Programming for Computer-Aided Analysis of Structures&lt;br /&gt;
* http://expressionflow.com/category/object-oriented-programming/intro-to-oop-in-labview/&lt;br /&gt;
* http://dl.acm.org/citation.cfm?id=36164 Block Structure and Object Oriented Languages&lt;br /&gt;
* http://en.wikipedia.org/wiki/Smalltalk Smalltalk&lt;br /&gt;
* http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure Necessary?&lt;br /&gt;
* http://www.php-developer.org/learn-php-oop-object-oriented-programming-for-beginners-tutorial/ PHP&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50578</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50578"/>
		<updated>2011-09-25T14:23:48Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* What is Object-Oriented Programming? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the code from JAVA language that is swapping two numbers by calling a new function swap from the main and by using a temporary variable temp. First the two numbers to be swapped are entered, then the swap function is called where it uses the temporary variable to swap both the numbers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 public class SwapElementsExample &lt;br /&gt;
 {&lt;br /&gt;
 &lt;br /&gt;
  public static void main(String[] args) &lt;br /&gt;
  {&lt;br /&gt;
 &lt;br /&gt;
   int num1 = 10;&lt;br /&gt;
   int num2 = 20;&lt;br /&gt;
 &lt;br /&gt;
   System.out.println(&amp;quot;Before Swapping&amp;quot;);&lt;br /&gt;
   System.out.println(&amp;quot;Value of num1 is :&amp;quot; + num1);&lt;br /&gt;
   System.out.println(&amp;quot;Value of num2 is :&amp;quot; +num2);&lt;br /&gt;
 &lt;br /&gt;
   //swap the value&lt;br /&gt;
   swap(num1, num2);&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  private static void swap(int num1, int num2) &lt;br /&gt;
  {&lt;br /&gt;
 &lt;br /&gt;
   int temp = num1;&lt;br /&gt;
   num1 = num2;&lt;br /&gt;
   num2 = temp;&lt;br /&gt;
 &lt;br /&gt;
   System.out.println(&amp;quot;After Swapping&amp;quot;);&lt;br /&gt;
   System.out.println(&amp;quot;Value of num1 is :&amp;quot; + num1);&lt;br /&gt;
   System.out.println(&amp;quot;Value of num2 is :&amp;quot; +num2);&lt;br /&gt;
 &lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Block_(programming) Block Programming&lt;br /&gt;
* http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers Learning Object-Oriented Programming&lt;br /&gt;
* http://www.cplusplus.com/doc/tutorial/control/ C++&lt;br /&gt;
* http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Contrast Structured and OOP&lt;br /&gt;
* http://www.cs.rpi.edu/~szymansk/OOF90/F90_Objects.html&lt;br /&gt;
* http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs Object Oriented Programming&lt;br /&gt;
* http://www.ibiblio.org/g2swap/byteofpython/read/oops.html&lt;br /&gt;
* http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview&lt;br /&gt;
* http://www.triconsole.com/php/oop.php&lt;br /&gt;
* http://www.wisegeek.com/what-is-structured-programming.htm What is Structured Programming&lt;br /&gt;
* http://ascelibrary.org/cpo/resource/1/jccee5/v18/i3/p226_s1 Object-Oriented Paradigm in Programming for Computer-Aided Analysis of Structures&lt;br /&gt;
* http://expressionflow.com/category/object-oriented-programming/intro-to-oop-in-labview/&lt;br /&gt;
* http://dl.acm.org/citation.cfm?id=36164 Block Structure and Object Oriented Languages&lt;br /&gt;
* http://en.wikipedia.org/wiki/Smalltalk Smalltalk&lt;br /&gt;
* http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure Necessary?&lt;br /&gt;
* http://www.php-developer.org/learn-php-oop-object-oriented-programming-for-beginners-tutorial/ PHP&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50577</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_vs&amp;diff=50577"/>
		<updated>2011-09-25T14:07:09Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* What is a Block-Structured Programming? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
The code snippet in [http://en.wikipedia.org/wiki/ALGOL ALGOL] below is an example which calculates the arithmetic mean by taking the number of samples and then asking for the values of the samples. It is block structured because of its modularized sections of code with the &amp;quot;begin&amp;quot; and &amp;quot;end&amp;quot; keywords. The scope of variables depends on where they are declared in the program. For example, N is an integer that has scope throughout the code whereas the array, sum, avg variables have the scope after reading the number of samples and the val variable has the local scope inside the for loop where the values are read for each sample.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
An example of the code from JAVA language is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Class HelloWorld&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     System.out.println(&amp;quot;Hello world!&amp;quot;):&lt;br /&gt;
     System.out.println(&amp;quot;Arguments you have entered is:&amp;quot;);&lt;br /&gt;
     if (args.length&amp;gt;0)&lt;br /&gt;
     {&lt;br /&gt;
       for(int i=0;i&amp;lt;args.length;i++)&lt;br /&gt;
       {&lt;br /&gt;
         System.out.print(args[i]+&amp;quot; &amp;quot;);&lt;br /&gt;
       }&lt;br /&gt;
       System.out.println();&lt;br /&gt;
     }&lt;br /&gt;
    else&lt;br /&gt;
       System.out.println(&amp;quot;&amp;lt;&amp;lt;No Arguments&amp;gt;&amp;gt;&amp;quot;);&lt;br /&gt;
    } &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Block_(programming) Block Programming&lt;br /&gt;
* http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers Learning Object-Oriented Programming&lt;br /&gt;
* http://www.cplusplus.com/doc/tutorial/control/ C++&lt;br /&gt;
* http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Contrast Structured and OOP&lt;br /&gt;
* http://www.cs.rpi.edu/~szymansk/OOF90/F90_Objects.html&lt;br /&gt;
* http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs Object Oriented Programming&lt;br /&gt;
* http://www.ibiblio.org/g2swap/byteofpython/read/oops.html&lt;br /&gt;
* http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview&lt;br /&gt;
* http://www.triconsole.com/php/oop.php&lt;br /&gt;
* http://www.wisegeek.com/what-is-structured-programming.htm What is Structured Programming&lt;br /&gt;
* http://ascelibrary.org/cpo/resource/1/jccee5/v18/i3/p226_s1 Object-Oriented Paradigm in Programming for Computer-Aided Analysis of Structures&lt;br /&gt;
* http://expressionflow.com/category/object-oriented-programming/intro-to-oop-in-labview/&lt;br /&gt;
* http://dl.acm.org/citation.cfm?id=36164 Block Structure and Object Oriented Languages&lt;br /&gt;
* http://en.wikipedia.org/wiki/Smalltalk Smalltalk&lt;br /&gt;
* http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure Necessary?&lt;br /&gt;
* http://www.php-developer.org/learn-php-oop-object-oriented-programming-for-beginners-tutorial/ PHP&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=48825</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=48825"/>
		<updated>2011-09-16T14:34:45Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* What is a Block-Structured Programming? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
An example of a block in [http://en.wikipedia.org/wiki/ALGOL ALGOL] looks as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
An example of the code from JAVA language is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Class HelloWorld&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     System.out.println(&amp;quot;Hello world!&amp;quot;):&lt;br /&gt;
     System.out.println(&amp;quot;Arguments you have entered is:&amp;quot;);&lt;br /&gt;
     if (args.length&amp;gt;0)&lt;br /&gt;
     {&lt;br /&gt;
       for(int i=0;i&amp;lt;args.length;i++)&lt;br /&gt;
       {&lt;br /&gt;
         System.out.print(args[i]+&amp;quot; &amp;quot;);&lt;br /&gt;
       }&lt;br /&gt;
       System.out.println();&lt;br /&gt;
     }&lt;br /&gt;
    else&lt;br /&gt;
       System.out.println(&amp;quot;&amp;lt;&amp;lt;No Arguments&amp;gt;&amp;gt;&amp;quot;);&lt;br /&gt;
    } &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Block_(programming) Block Programming&lt;br /&gt;
* http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers Learning Object-Oriented Programming&lt;br /&gt;
* http://www.cplusplus.com/doc/tutorial/control/ C++&lt;br /&gt;
* http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Contrast Structured and OOP&lt;br /&gt;
* http://www.cs.rpi.edu/~szymansk/OOF90/F90_Objects.html&lt;br /&gt;
* http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs Object Oriented Programming&lt;br /&gt;
* http://www.ibiblio.org/g2swap/byteofpython/read/oops.html&lt;br /&gt;
* http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview&lt;br /&gt;
* http://www.triconsole.com/php/oop.php&lt;br /&gt;
* http://www.wisegeek.com/what-is-structured-programming.htm What is Structured Programming&lt;br /&gt;
* http://ascelibrary.org/cpo/resource/1/jccee5/v18/i3/p226_s1 Object-Oriented Paradigm in Programming for Computer-Aided Analysis of Structures&lt;br /&gt;
* http://expressionflow.com/category/object-oriented-programming/intro-to-oop-in-labview/&lt;br /&gt;
* http://dl.acm.org/citation.cfm?id=36164 Block Structure and Object Oriented Languages&lt;br /&gt;
* http://en.wikipedia.org/wiki/Smalltalk Smalltalk&lt;br /&gt;
* http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure Necessary?&lt;br /&gt;
* http://www.php-developer.org/learn-php-oop-object-oriented-programming-for-beginners-tutorial/ PHP&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=48824</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=48824"/>
		<updated>2011-09-16T14:29:55Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* What is a Block-Structured Programming? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Block_(programming)]Block programming &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example of a block in [http://en.wikipedia.org/wiki/ALGOL ALGOL] looks as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
An example of the code from JAVA language is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Class HelloWorld&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     System.out.println(&amp;quot;Hello world!&amp;quot;):&lt;br /&gt;
     System.out.println(&amp;quot;Arguments you have entered is:&amp;quot;);&lt;br /&gt;
     if (args.length&amp;gt;0)&lt;br /&gt;
     {&lt;br /&gt;
       for(int i=0;i&amp;lt;args.length;i++)&lt;br /&gt;
       {&lt;br /&gt;
         System.out.print(args[i]+&amp;quot; &amp;quot;);&lt;br /&gt;
       }&lt;br /&gt;
       System.out.println();&lt;br /&gt;
     }&lt;br /&gt;
    else&lt;br /&gt;
       System.out.println(&amp;quot;&amp;lt;&amp;lt;No Arguments&amp;gt;&amp;gt;&amp;quot;);&lt;br /&gt;
    } &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Block_(programming) Block Programming&lt;br /&gt;
* http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers Learning Object-Oriented Programming&lt;br /&gt;
* http://www.cplusplus.com/doc/tutorial/control/ C++&lt;br /&gt;
* http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Contrast Structured and OOP&lt;br /&gt;
* http://www.cs.rpi.edu/~szymansk/OOF90/F90_Objects.html&lt;br /&gt;
* http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs Object Oriented Programming&lt;br /&gt;
* http://www.ibiblio.org/g2swap/byteofpython/read/oops.html&lt;br /&gt;
* http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview&lt;br /&gt;
* http://www.triconsole.com/php/oop.php&lt;br /&gt;
* http://www.wisegeek.com/what-is-structured-programming.htm What is Structured Programming&lt;br /&gt;
* http://ascelibrary.org/cpo/resource/1/jccee5/v18/i3/p226_s1 Object-Oriented Paradigm in Programming for Computer-Aided Analysis of Structures&lt;br /&gt;
* http://expressionflow.com/category/object-oriented-programming/intro-to-oop-in-labview/&lt;br /&gt;
* http://dl.acm.org/citation.cfm?id=36164 Block Structure and Object Oriented Languages&lt;br /&gt;
* http://en.wikipedia.org/wiki/Smalltalk Smalltalk&lt;br /&gt;
* http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure Necessary?&lt;br /&gt;
* http://www.php-developer.org/learn-php-oop-object-oriented-programming-for-beginners-tutorial/ PHP&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=48820</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=48820"/>
		<updated>2011-09-16T13:48:08Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
Block structured languages are those languages that have have a syntax of enclosed structures between bracketed keywords. The object oriented languages are those which are organized around data rather than logic. The article presented mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
An example of a block in [http://en.wikipedia.org/wiki/ALGOL ALGOL] looks as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
An example of the code from JAVA language is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Class HelloWorld&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     System.out.println(&amp;quot;Hello world!&amp;quot;):&lt;br /&gt;
     System.out.println(&amp;quot;Arguments you have entered is:&amp;quot;);&lt;br /&gt;
     if (args.length&amp;gt;0)&lt;br /&gt;
     {&lt;br /&gt;
       for(int i=0;i&amp;lt;args.length;i++)&lt;br /&gt;
       {&lt;br /&gt;
         System.out.print(args[i]+&amp;quot; &amp;quot;);&lt;br /&gt;
       }&lt;br /&gt;
       System.out.println();&lt;br /&gt;
     }&lt;br /&gt;
    else&lt;br /&gt;
       System.out.println(&amp;quot;&amp;lt;&amp;lt;No Arguments&amp;gt;&amp;gt;&amp;quot;);&lt;br /&gt;
    } &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Block_(programming) Block Programming&lt;br /&gt;
* http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers Learning Object-Oriented Programming&lt;br /&gt;
* http://www.cplusplus.com/doc/tutorial/control/ C++&lt;br /&gt;
* http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Contrast Structured and OOP&lt;br /&gt;
* http://www.cs.rpi.edu/~szymansk/OOF90/F90_Objects.html&lt;br /&gt;
* http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs Object Oriented Programming&lt;br /&gt;
* http://www.ibiblio.org/g2swap/byteofpython/read/oops.html&lt;br /&gt;
* http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview&lt;br /&gt;
* http://www.triconsole.com/php/oop.php&lt;br /&gt;
* http://www.wisegeek.com/what-is-structured-programming.htm What is Structured Programming&lt;br /&gt;
* http://ascelibrary.org/cpo/resource/1/jccee5/v18/i3/p226_s1 Object-Oriented Paradigm in Programming for Computer-Aided Analysis of Structures&lt;br /&gt;
* http://expressionflow.com/category/object-oriented-programming/intro-to-oop-in-labview/&lt;br /&gt;
* http://dl.acm.org/citation.cfm?id=36164 Block Structure and Object Oriented Languages&lt;br /&gt;
* http://en.wikipedia.org/wiki/Smalltalk Smalltalk&lt;br /&gt;
* http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure Necessary?&lt;br /&gt;
* http://www.php-developer.org/learn-php-oop-object-oriented-programming-for-beginners-tutorial/ PHP&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=48788</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=48788"/>
		<updated>2011-09-16T06:28:23Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
An example of a block in [http://en.wikipedia.org/wiki/ALGOL ALGOL] looks as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
An example of the code from JAVA language is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Class HelloWorld&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     System.out.println(&amp;quot;Hello world!&amp;quot;):&lt;br /&gt;
     System.out.println(&amp;quot;Arguments you have entered is:&amp;quot;);&lt;br /&gt;
     if (args.length&amp;gt;0)&lt;br /&gt;
     {&lt;br /&gt;
       for(int i=0;i&amp;lt;args.length;i++)&lt;br /&gt;
       {&lt;br /&gt;
         System.out.print(args[i]+&amp;quot; &amp;quot;);&lt;br /&gt;
       }&lt;br /&gt;
       System.out.println();&lt;br /&gt;
     }&lt;br /&gt;
    else&lt;br /&gt;
       System.out.println(&amp;quot;&amp;lt;&amp;lt;No Arguments&amp;gt;&amp;gt;&amp;quot;);&lt;br /&gt;
    } &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of [http://en.wiktionary.org/wiki/tramp_data tramp data].&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to the above needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.&lt;br /&gt;
&lt;br /&gt;
* Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
*The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. &lt;br /&gt;
&lt;br /&gt;
The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. &lt;br /&gt;
*Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. &lt;br /&gt;
*Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. &lt;br /&gt;
*Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. &lt;br /&gt;
*Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
&lt;br /&gt;
'''Usage of Blocks in Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in Java:'''&lt;br /&gt;
&lt;br /&gt;
Java also uses blocks in many situations like in conditional structures like if and else, looping constructs like &amp;quot;while&amp;quot;, &amp;quot;for&amp;quot; etc.&lt;br /&gt;
Let us for instance consider the while statement.The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 while (expression) {&lt;br /&gt;
     statement(s)&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following PrintCount program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 class PrintCount {&lt;br /&gt;
     public static void main(String[] args){&lt;br /&gt;
          int count = 1;&lt;br /&gt;
          while (count &amp;lt; 11) {&lt;br /&gt;
               System.out.println(&amp;quot;Count is: &amp;quot; + count);&lt;br /&gt;
               count++;&lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The statements included in the while block are blocks of code and cannot be avoided in situations such as these.This clearly proves that usage of blocks is not pertinent only to Block Structured Languages but also applies to Object Oriented languages as well.&lt;br /&gt;
Hence, blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus in conclusion we can say that, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code. Although Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are preferred in the present scenario because of the above mentioned features. The more complicated the project, the easier it is to leverage the strengths of object oriented design.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Block_(programming) Block Programming&lt;br /&gt;
* http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers Learning Object-Oriented Programming&lt;br /&gt;
* http://www.cplusplus.com/doc/tutorial/control/ C++&lt;br /&gt;
* http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Contrast Structured and OOP&lt;br /&gt;
* http://www.cs.rpi.edu/~szymansk/OOF90/F90_Objects.html&lt;br /&gt;
* http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs Object Oriented Programming&lt;br /&gt;
* http://www.ibiblio.org/g2swap/byteofpython/read/oops.html&lt;br /&gt;
* http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview&lt;br /&gt;
* http://www.triconsole.com/php/oop.php&lt;br /&gt;
* http://www.wisegeek.com/what-is-structured-programming.htm What is Structured Programming&lt;br /&gt;
* http://ascelibrary.org/cpo/resource/1/jccee5/v18/i3/p226_s1 Object-Oriented Paradigm in Programming for Computer-Aided Analysis of Structures&lt;br /&gt;
* http://expressionflow.com/category/object-oriented-programming/intro-to-oop-in-labview/&lt;br /&gt;
* http://dl.acm.org/citation.cfm?id=36164 Block Structure and Object Oriented Languages&lt;br /&gt;
* http://en.wikipedia.org/wiki/Smalltalk Smalltalk&lt;br /&gt;
* http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure Necessary?&lt;br /&gt;
* http://www.php-developer.org/learn-php-oop-object-oriented-programming-for-beginners-tutorial/ PHP&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=47753</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=47753"/>
		<updated>2011-09-08T17:52:49Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
An example of a block in [http://en.wikipedia.org/wiki/ALGOL ALGOL] looks as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
An example of the code from JAVA language is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Class HelloWorld&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     System.out.println(&amp;quot;Hello world!&amp;quot;):&lt;br /&gt;
     System.out.println(&amp;quot;Arguments you have entered is:&amp;quot;);&lt;br /&gt;
     if (args.length&amp;gt;0)&lt;br /&gt;
     {&lt;br /&gt;
       for(int i=0;i&amp;lt;args.length;i++)&lt;br /&gt;
       {&lt;br /&gt;
         System.out.print(args[i]+&amp;quot; &amp;quot;);&lt;br /&gt;
       }&lt;br /&gt;
       System.out.println();&lt;br /&gt;
     }&lt;br /&gt;
    else&lt;br /&gt;
       System.out.println(&amp;quot;&amp;lt;&amp;lt;No Arguments&amp;gt;&amp;gt;&amp;quot;);&lt;br /&gt;
    } &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of &amp;quot;tramp data&amp;quot;&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to these needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in CPP:'''&lt;br /&gt;
&lt;br /&gt;
C++ also uses blocks in many situations like in conditional structures like if and else.&lt;br /&gt;
The if keyword is used to execute a statement or block only if a condition is fulfilled. Its form is:&lt;br /&gt;
&lt;br /&gt;
if (condition) statement&lt;br /&gt;
&lt;br /&gt;
Where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues right after this conditional structure.&lt;br /&gt;
For example, the following code fragment prints x is 100 only if the value stored in the x variable is indeed 100:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  if (x == 100)&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;x is 100&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we want more than a single statement to be executed in case that the condition is true we can specify a block using braces { }:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 if (x == 100)&lt;br /&gt;
 {&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;x is &amp;quot;;&lt;br /&gt;
   cout &amp;lt;&amp;lt; x;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence, as seen above blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus we can say that though Block Structured languages and Object Oriented languages have their own advantages, Object oriented languages are definitely better and are preferred in the present scenario because of the above mentioned features. Also as we have seen above, the structure of a block can be used in Object Oriented programming as it provides a better understanding of the code.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Block_(programming) Block Programming&lt;br /&gt;
* http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers Learning Object-Oriented Programming&lt;br /&gt;
* http://www.cplusplus.com/doc/tutorial/control/ C++&lt;br /&gt;
* http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Contrast Structured and OOP&lt;br /&gt;
* http://www.cs.rpi.edu/~szymansk/OOF90/F90_Objects.html&lt;br /&gt;
* http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs Object Oriented Programming&lt;br /&gt;
* http://www.ibiblio.org/g2swap/byteofpython/read/oops.html&lt;br /&gt;
* http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview&lt;br /&gt;
* http://www.triconsole.com/php/oop.php&lt;br /&gt;
* http://www.wisegeek.com/what-is-structured-programming.htm What is Structured Programming&lt;br /&gt;
* http://ascelibrary.org/cpo/resource/1/jccee5/v18/i3/p226_s1 Object-Oriented Paradigm in Programming for Computer-Aided Analysis of Structures&lt;br /&gt;
* http://expressionflow.com/category/object-oriented-programming/intro-to-oop-in-labview/&lt;br /&gt;
* http://dl.acm.org/citation.cfm?id=36164 Block Structure and Object Oriented Languages&lt;br /&gt;
* http://en.wikipedia.org/wiki/Smalltalk Smalltalk&lt;br /&gt;
* http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure Necessary?&lt;br /&gt;
* http://www.php-developer.org/learn-php-oop-object-oriented-programming-for-beginners-tutorial/ PHP&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=47740</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=47740"/>
		<updated>2011-09-08T17:43:51Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* What is a Block-Structured Programming? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements enclosed within the bracketed keywords like if....fi. &lt;br /&gt;
&lt;br /&gt;
A block structured programming languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
&lt;br /&gt;
An example of a block in [http://en.wikipedia.org/wiki/ALGOL ALGOL] looks as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
An example of the code from JAVA language is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Class HelloWorld&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     System.out.println(&amp;quot;Hello world!&amp;quot;):&lt;br /&gt;
     System.out.println(&amp;quot;Arguments you have entered is:&amp;quot;);&lt;br /&gt;
     if (args.length&amp;gt;0)&lt;br /&gt;
     {&lt;br /&gt;
       for(int i=0;i&amp;lt;args.length;i++)&lt;br /&gt;
       {&lt;br /&gt;
         System.out.print(args[i]+&amp;quot; &amp;quot;);&lt;br /&gt;
       }&lt;br /&gt;
       System.out.println();&lt;br /&gt;
     }&lt;br /&gt;
    else&lt;br /&gt;
       System.out.println(&amp;quot;&amp;lt;&amp;lt;No Arguments&amp;gt;&amp;gt;&amp;quot;);&lt;br /&gt;
    } &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of &amp;quot;tramp data&amp;quot;&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to these needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in CPP:'''&lt;br /&gt;
&lt;br /&gt;
C++ also uses blocks in many situations like in conditional structures like if and else.&lt;br /&gt;
The if keyword is used to execute a statement or block only if a condition is fulfilled. Its form is:&lt;br /&gt;
&lt;br /&gt;
if (condition) statement&lt;br /&gt;
&lt;br /&gt;
Where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues right after this conditional structure.&lt;br /&gt;
For example, the following code fragment prints x is 100 only if the value stored in the x variable is indeed 100:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  if (x == 100)&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;x is 100&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we want more than a single statement to be executed in case that the condition is true we can specify a block using braces { }:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 if (x == 100)&lt;br /&gt;
 {&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;x is &amp;quot;;&lt;br /&gt;
   cout &amp;lt;&amp;lt; x;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence, as seen above blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus we can say that though these languages have their own advantages, Object oriented languages are preferred for the present scenario because of the above seen features.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Block_(programming) Block Programming&lt;br /&gt;
* http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers Learning Object-Oriented Programming&lt;br /&gt;
* http://www.cplusplus.com/doc/tutorial/control/ C++&lt;br /&gt;
* http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Contrast Structured and OOP&lt;br /&gt;
* http://www.cs.rpi.edu/~szymansk/OOF90/F90_Objects.html&lt;br /&gt;
* http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs Object Oriented Programming&lt;br /&gt;
* http://www.ibiblio.org/g2swap/byteofpython/read/oops.html&lt;br /&gt;
* http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview&lt;br /&gt;
* http://www.triconsole.com/php/oop.php&lt;br /&gt;
* http://www.wisegeek.com/what-is-structured-programming.htm What is Structured Programming&lt;br /&gt;
* http://ascelibrary.org/cpo/resource/1/jccee5/v18/i3/p226_s1 Object-Oriented Paradigm in Programming for Computer-Aided Analysis of Structures&lt;br /&gt;
* http://expressionflow.com/category/object-oriented-programming/intro-to-oop-in-labview/&lt;br /&gt;
* http://dl.acm.org/citation.cfm?id=36164 Block Structure and Object Oriented Languages&lt;br /&gt;
* http://en.wikipedia.org/wiki/Smalltalk Smalltalk&lt;br /&gt;
* http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure Necessary?&lt;br /&gt;
* http://www.php-developer.org/learn-php-oop-object-oriented-programming-for-beginners-tutorial/ PHP&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=47736</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=47736"/>
		<updated>2011-09-08T17:41:22Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: /* What is Object-Oriented Programming? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements. &lt;br /&gt;
&lt;br /&gt;
A block structured programming languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
Block structured languages have a syntax such that the structures are enclosed within the bracketed keywords like if....fi in [http://en.wikipedia.org/wiki/ALGOL ALGOL] language.&lt;br /&gt;
&lt;br /&gt;
An example of a block in [http://en.wikipedia.org/wiki/ALGOL ALGOL] looks as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
An example of the code from JAVA language is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Class HelloWorld&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     System.out.println(&amp;quot;Hello world!&amp;quot;):&lt;br /&gt;
     System.out.println(&amp;quot;Arguments you have entered is:&amp;quot;);&lt;br /&gt;
     if (args.length&amp;gt;0)&lt;br /&gt;
     {&lt;br /&gt;
       for(int i=0;i&amp;lt;args.length;i++)&lt;br /&gt;
       {&lt;br /&gt;
         System.out.print(args[i]+&amp;quot; &amp;quot;);&lt;br /&gt;
       }&lt;br /&gt;
       System.out.println();&lt;br /&gt;
     }&lt;br /&gt;
    else&lt;br /&gt;
       System.out.println(&amp;quot;&amp;lt;&amp;lt;No Arguments&amp;gt;&amp;gt;&amp;quot;);&lt;br /&gt;
    } &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of &amp;quot;tramp data&amp;quot;&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to these needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in CPP:'''&lt;br /&gt;
&lt;br /&gt;
C++ also uses blocks in many situations like in conditional structures like if and else.&lt;br /&gt;
The if keyword is used to execute a statement or block only if a condition is fulfilled. Its form is:&lt;br /&gt;
&lt;br /&gt;
if (condition) statement&lt;br /&gt;
&lt;br /&gt;
Where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues right after this conditional structure.&lt;br /&gt;
For example, the following code fragment prints x is 100 only if the value stored in the x variable is indeed 100:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  if (x == 100)&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;x is 100&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we want more than a single statement to be executed in case that the condition is true we can specify a block using braces { }:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 if (x == 100)&lt;br /&gt;
 {&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;x is &amp;quot;;&lt;br /&gt;
   cout &amp;lt;&amp;lt; x;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence, as seen above blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus we can say that though these languages have their own advantages, Object oriented languages are preferred for the present scenario because of the above seen features.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Block_(programming) Block Programming&lt;br /&gt;
* http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers Learning Object-Oriented Programming&lt;br /&gt;
* http://www.cplusplus.com/doc/tutorial/control/ C++&lt;br /&gt;
* http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Contrast Structured and OOP&lt;br /&gt;
* http://www.cs.rpi.edu/~szymansk/OOF90/F90_Objects.html&lt;br /&gt;
* http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs Object Oriented Programming&lt;br /&gt;
* http://www.ibiblio.org/g2swap/byteofpython/read/oops.html&lt;br /&gt;
* http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview&lt;br /&gt;
* http://www.triconsole.com/php/oop.php&lt;br /&gt;
* http://www.wisegeek.com/what-is-structured-programming.htm What is Structured Programming&lt;br /&gt;
* http://ascelibrary.org/cpo/resource/1/jccee5/v18/i3/p226_s1 Object-Oriented Paradigm in Programming for Computer-Aided Analysis of Structures&lt;br /&gt;
* http://expressionflow.com/category/object-oriented-programming/intro-to-oop-in-labview/&lt;br /&gt;
* http://dl.acm.org/citation.cfm?id=36164 Block Structure and Object Oriented Languages&lt;br /&gt;
* http://en.wikipedia.org/wiki/Smalltalk Smalltalk&lt;br /&gt;
* http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure Necessary?&lt;br /&gt;
* http://www.php-developer.org/learn-php-oop-object-oriented-programming-for-beginners-tutorial/ PHP&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=47735</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e vs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_vs&amp;diff=47735"/>
		<updated>2011-09-08T17:40:34Z</updated>

		<summary type="html">&lt;p&gt;Vvarre: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch1 1e vs&lt;br /&gt;
----&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article mainly compares and contrasts [http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] and [http://en.wikipedia.org/wiki/Object-oriented_programming Object-Oriented programming] and focuses on the advantages of Object-Oriented programming over Block-structured programming. It also emphasizes on the usage of block structures in Object-Oriented programming.&lt;br /&gt;
&lt;br /&gt;
==What is a Block-Structured Programming?==&lt;br /&gt;
A block is a section of [http://en.wikipedia.org/wiki/Computer_code code] which is grouped together and consists of one or more declarations and statements. &lt;br /&gt;
&lt;br /&gt;
A block structured programming languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/Pascal_(programming_language PASCAL],[http://en.wikipedia.org/wiki/Fortran FORTRAN] etc..&lt;br /&gt;
Block structured languages have a syntax such that the structures are enclosed within the bracketed keywords like if....fi in [http://en.wikipedia.org/wiki/ALGOL ALGOL] language.&lt;br /&gt;
&lt;br /&gt;
An example of a block in [http://en.wikipedia.org/wiki/ALGOL ALGOL] looks as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 begin&lt;br /&gt;
   integer N;&lt;br /&gt;
   Read Int(N);&lt;br /&gt;
   begin&lt;br /&gt;
     real array Data[1:N]&lt;br /&gt;
     real sum,avg;&lt;br /&gt;
     sum:=0;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
        begin real val;&lt;br /&gt;
         Read Real (val);&lt;br /&gt;
         Data[i]:=if val&amp;lt;0 then -val else val&lt;br /&gt;
        end;&lt;br /&gt;
     for i:=1 step 1 until N do&lt;br /&gt;
      sum:=sum+Data[i];&lt;br /&gt;
    avg:=sum/N;&lt;br /&gt;
    Print Real(avg);&lt;br /&gt;
   end &lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
&lt;br /&gt;
The essential composition of block structured programming tends to include three basic elements:&lt;br /&gt;
&lt;br /&gt;
*'''Concatenation:''' Concatenation is the element which is responsible for the logical sequence of the statements that make up the basics for the order to be executed. &lt;br /&gt;
&lt;br /&gt;
*'''Selection:''' Selection is the second element included in the process of structural programming which allows selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords like “if,” “then,” “endif,” or “switch” to identify the order as a logical executable.&lt;br /&gt;
&lt;br /&gt;
*'''Repetition:''' In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions. &lt;br /&gt;
&lt;br /&gt;
The exact nature of block structured programming will varies depending on the purpose and function of the program.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
*Restricted visibility to local identifiers.&lt;br /&gt;
&lt;br /&gt;
*Efficient use of storage.&lt;br /&gt;
&lt;br /&gt;
*Learning and programming of the language is both quick and relatively easy.&lt;br /&gt;
&lt;br /&gt;
*Turn around time is likely to be quite rapid, since the number of de-bugging runs is usually small.&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
*Focus is given on the procedure (logic) and not to the data on which these procedures operate.&lt;br /&gt;
*The data need to be global but global data makes the code complex.&lt;br /&gt;
*As the code size grows, maintaining code becomes difficult and sometimes impossible.&lt;br /&gt;
*Structured programming language is not ‘modular’ i.e., no reuse of code leading to redundant code.&lt;br /&gt;
&lt;br /&gt;
==What is Object-Oriented Programming?==&lt;br /&gt;
Object-Oriented programming is a programming paradigm using [http://en.wikipedia.org/wiki/Object_(computer_science) &amp;quot;objects&amp;quot;]- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. The programming techniques include features like [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism] and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance].&lt;br /&gt;
It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.&lt;br /&gt;
&lt;br /&gt;
An example of the code from JAVA language is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Class HelloWorld&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     System.out.println(&amp;quot;Hello world!&amp;quot;):&lt;br /&gt;
     System.out.println(&amp;quot;Arguments you have entered is:&amp;quot;);&lt;br /&gt;
     if (args.length&amp;gt;0)&lt;br /&gt;
     {&lt;br /&gt;
       for(int i=0;i&amp;lt;args.length;i++)&lt;br /&gt;
       {&lt;br /&gt;
         System.out.print(args[i]+&amp;quot; &amp;quot;);&lt;br /&gt;
       }&lt;br /&gt;
       System.out.println();&lt;br /&gt;
     }&lt;br /&gt;
    else&lt;br /&gt;
       System.out.println(&amp;quot;&amp;lt;&amp;lt;No Arguments&amp;gt;&amp;gt;&amp;quot;);&lt;br /&gt;
    } &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Features===&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]:''' Inheritance establishes relationships among [http://en.wikipedia.org/wiki/Class_(computer_science) classes] where it helps the [http://en.wikipedia.org/wiki/Object_(computer_science) objects] to work together and makes it easy to extend  existing structures to produce new structures with slightly different behavior.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation]:''' Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Data_abstraction Data Abstraction]:''' Data abstration is the development of [http://en.wikipedia.org/wiki/Class_(computer_science) classes], [http://en.wikipedia.org/wiki/Object_(computer_science) objects], types in terms of their [http://en.wikipedia.org/wiki/Interface_(computer_science) interfaces] and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item and is used to manage complexity.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]:''' The concept of polymorphism can be explained as &amp;quot;one [http://en.wikipedia.org/wiki/Interface_(computer_science) interface], multiple [http://en.wikipedia.org/wiki/Method_(computer_science) methods]&amp;quot;. Polymorphism enables one entity to be used as as general category for different types of actions and there are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
*Simulates real-world event much more effectively &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code Code] is reusable thus less code may have to be written &lt;br /&gt;
*[http://en.wikipedia.org/wiki/Data Data] becomes active &lt;br /&gt;
*Better ability to create GUI (graphical user interface) applications &lt;br /&gt;
*Ability to reach their goals faster &lt;br /&gt;
*Programmers are able to produce faster, more accurate and better-written applications&lt;br /&gt;
&lt;br /&gt;
===Limitations===&lt;br /&gt;
&lt;br /&gt;
*Best suited for dynamic, interactive environments but becomes complicated for systems that don't require so much complexity.&lt;br /&gt;
* OOP provides only one representation (object-oriented).&lt;br /&gt;
* Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs.&lt;br /&gt;
&lt;br /&gt;
==Transition from Block Structured to OO Programming?==&lt;br /&gt;
===History===&lt;br /&gt;
Before the emergence of block structured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming.&lt;br /&gt;
&lt;br /&gt;
The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down method and was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: [http://en.wikipedia.org/wiki/Fortran FORTRAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/ALGOL ALGOL],[http://en.wikipedia.org/wiki/C_(programming_language) C] etc.&lt;br /&gt;
&lt;br /&gt;
	[http://en.wikipedia.org/wiki/Block_(programming) Block-Structured programming] remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:&lt;br /&gt;
&lt;br /&gt;
*In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.&lt;br /&gt;
&lt;br /&gt;
*Hierarchical structuring for data and procedures produced cumbersome code with large amounts of &amp;quot;tramp data&amp;quot;&lt;br /&gt;
&lt;br /&gt;
*Usage of global subprogram names and variables was recognized dangerous. &lt;br /&gt;
&lt;br /&gt;
*Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Why OOP?===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] was developed to respond to these needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.Object Oriented programming paradigm used &amp;quot;[http://en.wikipedia.org/wiki/Object_(computer_science) objects]&amp;quot; which were considered to be [http://en.wikipedia.org/wiki/Data_structure data structures] consisting of data fields and [http://en.wikipedia.org/wiki/Method_(computer_science) methods] together with their interactions. It also took the concept of [http://en.wikipedia.org/wiki/Subroutine subroutines] into a completely different zone. &lt;br /&gt;
&lt;br /&gt;
The main advantage of object oriented programming over other languages is that they enable programmers to create modules that need not be changed when a type of object is added. Software objects model real world objects and so the complexity is reduced and the program structure becomes very clear. The main features that make the object oriented better are modifiability, extensibility, maintainability, portability, scalability and reusability. Modifiability ensures that it is easy to make minor changes in the data representation or the procedures in an OO program. And any changes inside the class will not affect any other part of a program. Extensibility is adding new features or responding to changing operating environments by introducing new objects and modifying some existing ones. Maintanability is the feature where the objects can be maintained and the problems can be fixed easier. Reusability is a very important feature where the objects can be reused in different programs.&lt;br /&gt;
This makes OOP efficient for more complicated and flexible interactions. For many systems, an OO approach can speed development time since many objects are standard across systems and can be reused.&lt;br /&gt;
&lt;br /&gt;
Its programming techniques included features such as [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Message_passing messaging], [http://en.wikipedia.org/wiki/Module_(programming) modularity], [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism], and [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] which changed the face of programming for many people.&lt;br /&gt;
Some of the most commonly used [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming languages] are: [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/Java_(programming_language) Java] etc..&lt;br /&gt;
&lt;br /&gt;
==Comparision between Block Structured and OO Programming==&lt;br /&gt;
&lt;br /&gt;
'''Block-Structured languages vs. Object-oriented languages'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Properties&lt;br /&gt;
! Block-Structured languages&lt;br /&gt;
! Object-oriented languages&lt;br /&gt;
|-&lt;br /&gt;
| '''Primary focus'''&lt;br /&gt;
| The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines.&lt;br /&gt;
| The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.&lt;br /&gt;
|-&lt;br /&gt;
| '''Approach'''&lt;br /&gt;
| Block Structured languages follow a Top-down approach.&lt;br /&gt;
| Object Oriented languagues follow a Bottom-up approach.&lt;br /&gt;
|-&lt;br /&gt;
| '''Program design'''&lt;br /&gt;
| It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language.&lt;br /&gt;
| It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.&lt;br /&gt;
|-&lt;br /&gt;
| '''Data Protection'''&lt;br /&gt;
| These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions.&lt;br /&gt;
| Object-Oriented languages enforce &amp;quot;hiding&amp;quot; of data declared private in a class which can be accessed externally only through the public member functions of that class.&lt;br /&gt;
|-&lt;br /&gt;
| '''Creation of new data types'''&lt;br /&gt;
| It is tedious at the language level if the language does not support OOP.&lt;br /&gt;
| It is easily accomplished at this language level by defining a new class for the data type.&lt;br /&gt;
|-&lt;br /&gt;
| '''Dynamic Binding'''&lt;br /&gt;
| Instance variables, static variables, static overridden methods and overloaded methods are all bound at compile time.&lt;br /&gt;
| OOP enables polymorphism in which objects belonging to different types respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. So the exact behavior is determined at run-time (called late binding or dynamic binding).&lt;br /&gt;
|-&lt;br /&gt;
| '''Comprehension of the program''' &lt;br /&gt;
| Rigorous due to poor equivalence between the program variables and physical entities&lt;br /&gt;
| More apparent due to the close match between program classes and real-world classes.&lt;br /&gt;
|-&lt;br /&gt;
| '''Modularity'''&lt;br /&gt;
| Not as good since data structures and functions are not linked.&lt;br /&gt;
| Better since related data and functions are placed in the same module.&lt;br /&gt;
|- &lt;br /&gt;
| '''Complexity'''&lt;br /&gt;
| Simple to understand small programs but gets complicated when the code gets large.&lt;br /&gt;
| Easy to understand but can get complex in some cases by unnecessary abstraction.&lt;br /&gt;
|-&lt;br /&gt;
| '''Maintainance and extension of the program'''&lt;br /&gt;
| Requires high degree of knowledge of the entire program.&lt;br /&gt;
| Convenient due to enhanced modularity of the program.&lt;br /&gt;
|-&lt;br /&gt;
| '''Reusability of code'''&lt;br /&gt;
| Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module&lt;br /&gt;
| Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application&lt;br /&gt;
|-&lt;br /&gt;
| '''Examples'''&lt;br /&gt;
| C, ALGOL 60, PASCAL are examples of block-structured languages.&lt;br /&gt;
| C++ and JAVA are examples of object-oriented languages.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Usage of Block Structures in Object Oriented Programming==&lt;br /&gt;
In most programming languages, a statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block). In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block. A block is a group of statements separated by a semicolon(;) but grouped together in a block enclosed in braces : {}: &lt;br /&gt;
&lt;br /&gt;
The basic building blocks of object oriented languages are considered as objects and classes. Class is the building block that encapsulates both data and functionality. The blocks in object oriented languages are used to concisely represent some code that would typically be written with loops in some other languages by using looping constructs such as 'if-else', 'while', 'for' etc.. in JAVA. Blocks allow control structure to be implemented using messages and polymorphism. Blocks are used to implement user-defined control structures, enumerators, visitors, pluggable behavior and many other patterns. In any object oriented language, all the programs are designed around functions or block of statements which manipulate data. These languages allow the programmers to think in terms of building blocks that are closer to what program actually will do. The well known programming quality criteria are programming speed, error correction speed, computation speed, readability and portability and the importance of a hierarchical description of systems. The use of encapsulation, inheritance and polymorphism fulfill the previously mentioned criteria without the need to incorporate complex kernels to control data flows. The block structure enables recycling blocks of program code. Once a block of program code is written, it can be reused in any number of programs unlike traditional programming. The methods which represent the operations are declared in the class blocks and the methods are implemented in the method blocks. &lt;br /&gt;
&lt;br /&gt;
Let us now see the usage of blocks in some Object Oriented Languages:&lt;br /&gt;
In Ruby, closure is called a block. &lt;br /&gt;
In an object instance variable (denoted with '@'), remember a block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def remember(&amp;amp;a_block)&lt;br /&gt;
  @block = a_block&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Invoke the above method, giving it a block which takes a name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 remember {|name| puts &amp;quot;Hello, #{name}!&amp;quot;}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
When the time is right (for the object) -- call the closure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 @block.call(&amp;quot;Jon&amp;quot;)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=&amp;gt; &amp;quot;Hello, Jon!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Iterating over enumerations and arrays using blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 array = [1, 'hi', 3.14]&lt;br /&gt;
 array.each {|item| puts item }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 array.each_index {|index| puts &amp;quot;#{index}: #{array[index]}&amp;quot; }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*'hi'&lt;br /&gt;
*3.14&lt;br /&gt;
&lt;br /&gt;
'''Usage of blocks in CPP:'''&lt;br /&gt;
&lt;br /&gt;
C++ also uses blocks in many situations like in conditional structures like if and else.&lt;br /&gt;
The if keyword is used to execute a statement or block only if a condition is fulfilled. Its form is:&lt;br /&gt;
&lt;br /&gt;
if (condition) statement&lt;br /&gt;
&lt;br /&gt;
Where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues right after this conditional structure.&lt;br /&gt;
For example, the following code fragment prints x is 100 only if the value stored in the x variable is indeed 100:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  if (x == 100)&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;x is 100&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we want more than a single statement to be executed in case that the condition is true we can specify a block using braces { }:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 if (x == 100)&lt;br /&gt;
 {&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;x is &amp;quot;;&lt;br /&gt;
   cout &amp;lt;&amp;lt; x;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence, as seen above blocks form an integral part of programming practice in most of the languagues, but the importance given to the blocks and functions or to the data depends based on the paradigm used.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus we can say that though these languages have their own advantages, Object oriented languages are preferred for the present scenario because of the above seen features.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[http://www.scribd.com/doc/35821664/Programming-Paradigms Programming Paradigms] &lt;br /&gt;
*[http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html History of OOP]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Function_overloading Function Overloading]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Method_overriding Method Overriding in Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Comparison_of_programming_languages Comparison of Programming languages]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Structured_programming Structured Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Block_(programming) Block Programming&lt;br /&gt;
* http://bulletin.sigchi.org/1997/october/papers/ross/ Structured Programmers Learning Object-Oriented Programming&lt;br /&gt;
* http://www.cplusplus.com/doc/tutorial/control/ C++&lt;br /&gt;
* http://askville.amazon.com/Compare-Contrast-Structured-programming-Object-Oriented/AnswerViewer.do?requestId=2470573 Contrast Structured and OOP&lt;br /&gt;
* http://www.cs.rpi.edu/~szymansk/OOF90/F90_Objects.html&lt;br /&gt;
* http://janeataylor.wordpress.com/2005/12/02/structured-vs-object-oriented-programming/ Structured vs Object Oriented Programming&lt;br /&gt;
* http://www.ibiblio.org/g2swap/byteofpython/read/oops.html&lt;br /&gt;
* http://www.delphibasics.co.uk/Article.asp?Name=OO Object Oriented Overview&lt;br /&gt;
* http://www.triconsole.com/php/oop.php&lt;br /&gt;
* http://www.wisegeek.com/what-is-structured-programming.htm What is Structured Programming&lt;br /&gt;
* http://ascelibrary.org/cpo/resource/1/jccee5/v18/i3/p226_s1 Object-Oriented Paradigm in Programming for Computer-Aided Analysis of Structures&lt;br /&gt;
* http://expressionflow.com/category/object-oriented-programming/intro-to-oop-in-labview/&lt;br /&gt;
* http://dl.acm.org/citation.cfm?id=36164 Block Structure and Object Oriented Languages&lt;br /&gt;
* http://en.wikipedia.org/wiki/Smalltalk Smalltalk&lt;br /&gt;
* http://drhanson.s3.amazonaws.com/storage/documents/blockstructure.pdf Is Block Structure Necessary?&lt;br /&gt;
* http://www.php-developer.org/learn-php-oop-object-oriented-programming-for-beginners-tutorial/ PHP&lt;/div&gt;</summary>
		<author><name>Vvarre</name></author>
	</entry>
</feed>