<?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=Svontel</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=Svontel"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Svontel"/>
	<updated>2026-05-18T09:43:01Z</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_4f_ss&amp;diff=52888</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52888"/>
		<updated>2011-10-20T03:48:13Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Illustration of Reflection in Java through Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Introduction &lt;br /&gt;
&lt;br /&gt;
Here we mainly focus on the lecture 11 of CSC 517 which is Reflection in Ruby. We also cover additional information on Reflection in Java, advantages,disadvantages of Reflection&lt;br /&gt;
== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler  because it is done using the language mechanisms ie &amp;lt;object&amp;gt;''.methods'' gives the list of methods. This is in  contrast  to reflection in java which is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).Using Reflection,we can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 100&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum &lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
#instance_of? is a method of Object.returns true if the object is an instance of the #given class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Difference between Fixnum and Bignum in Ruby'''&lt;br /&gt;
&lt;br /&gt;
A Fixnum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Fixnum.html&amp;lt;/ref&amp;gt; stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Bignum.html&amp;lt;/ref&amp;gt; objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would  overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a [http://en.wikipedia.org/wiki/Primitive_data_type primitive] and  [http://en.wikipedia.org/wiki/Reference_type reference]with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.In contrast to Fixnum objects,  objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
Example 4:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Comparable.html '''Comparable'''] which is  an  ancestor of the [http://ruby-doc.org/core-1.9.2/String.html String] class is a [http://en.wikipedia.org/wiki/Mixin mixin] .It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Object.html '''Object'''] is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/BasicObject.html '''Basic Object'''] is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Kernel.html '''Kernel'''] is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Integer.html '''Integer''']is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Numeric.html '''Numeric'''] is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example.&lt;br /&gt;
&lt;br /&gt;
'''Note''': It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end &lt;br /&gt;
#this is not a good practice.Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say ,size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in Ruby,assignments physically copy references, but not the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the super class of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method(as a symbol) has to be passed  as a parameter to 'method'&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,a reference to the method upcase of the String class is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference(m) can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol. Thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol &lt;br /&gt;
332648                                    # yielded same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Numerical_integration'''Quadrature integration''']&lt;br /&gt;
Here we compute the area under a curve say f(x) between two points say 'a' and 'b' by dividing the region under the curve into 'n' small rectangles with the heights parallel to the y axis.ie The length a to b is divided into n intervals.The area of each rectangle can be obtained by multiplying the height and the width.Height is the average of the values of f(x) at the two end points of the interval.Width is the length of the interval . Sum of the areas of all the rectangles gives us an approximation of the area of the area under f(x).&lt;br /&gt;
&lt;br /&gt;
Example: Suppose we need to compute the area under a curve described by some function,  x2 + 2x + 3 between say 10 and 20. &lt;br /&gt;
Then we can define a method poly for the Float class which returns the evaluation of the polynomial at that point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,end points between which the area is to be computed as the parameters &lt;br /&gt;
&amp;lt;pre&amp;gt;area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the   #points x=10 to x=20.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method  [http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_object.html#Object.method_missing method_missing]within the class definition provides this facility. This feature is not available in java.A class cast  [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Exception.html exception] is thrown whenever  a call to an undefined method is made. Ruby passes the name of the method called(which is unavailable) and the arguments passed to it as parameters to the method_missing  method . &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example : In the following example, class Duck defines 'quack' and 'method_missing' methods. When an object d of Duck is created and quack is invoked on it,it executes the quack of Duck(as expected). But when  'bark' is invoked on it, method_missing is invoked.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args) #handles calls to undefined methods&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark #bark is passed to method_missing since 'bark' method is not defined in Duck class.&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of an interesting usage of method_missing: “Roman method_missing”.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Roman &lt;br /&gt;
&lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    prev = nil #prev stores the previous value in roman string&lt;br /&gt;
    roman_string.to_s.upcase.split(//).reverse.inject(0) do |memo, digit|&lt;br /&gt;
     if digit_value = DIGITS[digit] #checks if the integer value is present in the hash&lt;br /&gt;
        if prev &amp;amp;&amp;amp; prev &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
        prev = digit_value&lt;br /&gt;
       end&lt;br /&gt;
      memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end    &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
 r=Roman.new&lt;br /&gt;
 r.XV&lt;br /&gt;
Output : =&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example we declare a class Roman containing a hash called DIGITS which maps  roman symbols with the corresponding integers. The class also contains the methods method_missing and roman_to_integer. When an object of Roman class( say r) is created and when we invoke method XV on r (ie r.XV) ,method_missing(:XV) is  invoked since the Roman class has no method named XV. Inside method_missing, the symbol is converted to &amp;quot;XV&amp;quot;(string) and  roman_to_str(&amp;quot;XV&amp;quot;) is called. In roman_to_str method, the string is converted to uppercase, split into an array and the array is reversed. The inject operator  then passes  each element (of the array)  and an accumulator value (memo)to the block.The initial value of  memo is 0.  For each element,if the previous digit_value exists and is greater than the current digit_value,the  current digit_value is added to memo. Else current digit_value is subtracted from memo.The result becomes the new value for memo. At the end of the iteration, the final value of memo is the integer value of the roman string.&lt;br /&gt;
&lt;br /&gt;
==Reflection in Java==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
* Which class does an object belong to?&lt;br /&gt;
* What is the description of a given class name?&lt;br /&gt;
* What are the fields in a given class?&lt;br /&gt;
* What is the type of a field?&lt;br /&gt;
* What are the methods in a class?&lt;br /&gt;
* What are the parameters of a method?&lt;br /&gt;
* What are the constructors of a given class? &lt;br /&gt;
* Constructing an object using a given constructor&lt;br /&gt;
* Invoking an object's method using such-and-such parameters&lt;br /&gt;
* Assigning a value to an object's field&lt;br /&gt;
* Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/package-summary.html java.lang.reflect].&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html 'Class']&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
class Fields-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Field.html 'Field'] class&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
methods-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Method.html 'Method'] class&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
constructors-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Constructor.html 'Constructor'] class&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
arrays-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Array.html 'Array'] class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + f.getName());&lt;br /&gt;
                 &lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; + f.getDeclaringClass());&lt;br /&gt;
                          &lt;br /&gt;
               System.out.println(&amp;quot;type = &amp;quot; + f.getType());&lt;br /&gt;
                 &lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; + ct.getDeclaringClass());&lt;br /&gt;
                           &lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot;  + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
                    &lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52885</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52885"/>
		<updated>2011-10-20T03:34:23Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Illustration of Reflection in Java through Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Introduction &lt;br /&gt;
&lt;br /&gt;
Here we mainly focus on the lecture 11 of CSC 517 which is Reflection in Ruby. We also cover additional information on Reflection in Java, advantages,disadvantages of Reflection&lt;br /&gt;
== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler  because it is done using the language mechanisms ie &amp;lt;object&amp;gt;''.methods'' gives the list of methods. This is in  contrast  to reflection in java which is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).Using Reflection,we can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 100&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum &lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
#instance_of? is a method of Object.returns true if the object is an instance of the #given class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Difference between Fixnum and Bignum in Ruby'''&lt;br /&gt;
&lt;br /&gt;
A Fixnum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Fixnum.html&amp;lt;/ref&amp;gt; stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Bignum.html&amp;lt;/ref&amp;gt; objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would  overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a [http://en.wikipedia.org/wiki/Primitive_data_type primitive] and  [http://en.wikipedia.org/wiki/Reference_type reference]with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.In contrast to Fixnum objects,  objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
Example 4:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Comparable.html '''Comparable'''] which is  an  ancestor of the [http://ruby-doc.org/core-1.9.2/String.html String] class is a [http://en.wikipedia.org/wiki/Mixin mixin] .It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Object.html '''Object'''] is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/BasicObject.html '''Basic Object'''] is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Kernel.html '''Kernel'''] is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Integer.html '''Integer''']is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Numeric.html '''Numeric'''] is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example.&lt;br /&gt;
&lt;br /&gt;
'''Note''': It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end &lt;br /&gt;
#this is not a good practice.Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say ,size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in Ruby,assignments physically copy references, but not the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the super class of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method(as a symbol) has to be passed  as a parameter to 'method'&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,a reference to the method upcase of the String class is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference(m) can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol. Thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol &lt;br /&gt;
332648                                    # yielded same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Numerical_integration'''Quadrature integration''']&lt;br /&gt;
Here we compute the area under a curve say f(x) between two points say 'a' and 'b' by dividing the region under the curve into 'n' small rectangles with the heights parallel to the y axis.ie The length a to b is divided into n intervals.The area of each rectangle can be obtained by multiplying the height and the width.Height is the average of the values of f(x) at the two end points of the interval.Width is the length of the interval . Sum of the areas of all the rectangles gives us an approximation of the area of the area under f(x).&lt;br /&gt;
&lt;br /&gt;
Example: Suppose we need to compute the area under a curve described by some function,  x2 + 2x + 3 between say 10 and 20. &lt;br /&gt;
Then we can define a method poly for the Float class which returns the evaluation of the polynomial at that point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,end points between which the area is to be computed as the parameters &lt;br /&gt;
&amp;lt;pre&amp;gt;area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the   #points x=10 to x=20.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method  [http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_object.html#Object.method_missing method_missing]within the class definition provides this facility. This feature is not available in java.A class cast  [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Exception.html exception] is thrown whenever  a call to an undefined method is made. Ruby passes the name of the method called(which is unavailable) and the arguments passed to it as parameters to the method_missing  method . &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example : In the following example, class Duck defines 'quack' and 'method_missing' methods. When an object d of Duck is created and quack is invoked on it,it executes the quack of Duck(as expected). But when  'bark' is invoked on it, method_missing is invoked.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args) #handles calls to undefined methods&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark #bark is passed to method_missing since 'bark' method is not defined in Duck class.&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of an interesting usage of method_missing: “Roman method_missing”.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Roman &lt;br /&gt;
&lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    prev = nil #prev stores the previous value in roman string&lt;br /&gt;
    roman_string.to_s.upcase.split(//).reverse.inject(0) do |memo, digit|&lt;br /&gt;
     if digit_value = DIGITS[digit] #checks if the integer value is present in the hash&lt;br /&gt;
        if prev &amp;amp;&amp;amp; prev &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
        prev = digit_value&lt;br /&gt;
       end&lt;br /&gt;
      memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end    &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
 r=Roman.new&lt;br /&gt;
 r.XV&lt;br /&gt;
Output : =&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example we declare a class Roman containing a hash called DIGITS which maps  roman symbols with the corresponding integers. The class also contains the methods method_missing and roman_to_integer. When an object of Roman class( say r) is created and when we invoke method XV on r (ie r.XV) ,method_missing(:XV) is  invoked since the Roman class has no method named XV. Inside method_missing, the symbol is converted to &amp;quot;XV&amp;quot;(string) and  roman_to_str(&amp;quot;XV&amp;quot;) is called. In roman_to_str method, the string is converted to uppercase, split into an array and the array is reversed. The inject operator  then passes  each element (of the array)  and an accumulator value (memo)to the block.The initial value of  memo is 0.  For each element,if the previous digit_value exists and is greater than the current digit_value,the  current digit_value is added to memo. Else current digit_value is subtracted from memo.The result becomes the new value for memo. At the end of the iteration, the final value of memo is the integer value of the roman string.&lt;br /&gt;
&lt;br /&gt;
==Reflection in Java==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
* Which class does an object belong to?&lt;br /&gt;
* What is the description of a given class name?&lt;br /&gt;
* What are the fields in a given class?&lt;br /&gt;
* What is the type of a field?&lt;br /&gt;
* What are the methods in a class?&lt;br /&gt;
* What are the parameters of a method?&lt;br /&gt;
* What are the constructors of a given class? &lt;br /&gt;
* Constructing an object using a given constructor&lt;br /&gt;
* Invoking an object's method using such-and-such parameters&lt;br /&gt;
* Assigning a value to an object's field&lt;br /&gt;
* Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/package-summary.html java.lang.reflect].&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html 'Class']&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
class Fields-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Field.html 'Field'] class&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
methods-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Method.html 'Method'] class&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
constructors-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Constructor.html 'Constructor'] class&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
arrays-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Array.html 'Array'] class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52874</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52874"/>
		<updated>2011-10-20T03:13:07Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Reflection in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Introduction &lt;br /&gt;
&lt;br /&gt;
Here we mainly focus on the lecture 11 of CSC 517 which is Reflection in Ruby. We also cover additional information on Reflection in Java, advantages,disadvantages of Reflection&lt;br /&gt;
== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler  because it is done using the language mechanisms ie &amp;lt;object&amp;gt;''.methods'' gives the list of methods. This is in  contrast  to reflection in java which is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).Using Reflection,we can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 100&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum &lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
#instance_of? is a method of Object.returns true if the object is an instance of the #given class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Difference between Fixnum and Bignum in Ruby'''&lt;br /&gt;
&lt;br /&gt;
A Fixnum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Fixnum.html&amp;lt;/ref&amp;gt; stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Bignum.html&amp;lt;/ref&amp;gt; objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would  overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a [http://en.wikipedia.org/wiki/Primitive_data_type primitive] and  [http://en.wikipedia.org/wiki/Reference_type reference]with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.In contrast to Fixnum objects,  objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
Example 4:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Comparable.html '''Comparable'''] which is  an  ancestor of the [http://ruby-doc.org/core-1.9.2/String.html String] class is a [http://en.wikipedia.org/wiki/Mixin mixin] .It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Object.html '''Object'''] is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/BasicObject.html '''Basic Object'''] is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Kernel.html '''Kernel'''] is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Integer.html '''Integer''']is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Numeric.html '''Numeric'''] is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example.&lt;br /&gt;
&lt;br /&gt;
'''Note''': It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end &lt;br /&gt;
#this is not a good practice.Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say ,size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in Ruby,assignments physically copy references, but not the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the super class of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method(as a symbol) has to be passed  as a parameter to 'method'&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,a reference to the method upcase of the String class is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference(m) can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol. Thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol &lt;br /&gt;
332648                                    # yielded same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Numerical_integration'''Quadrature integration''']&lt;br /&gt;
Here we compute the area under a curve say f(x) between two points say 'a' and 'b' by dividing the region under the curve into 'n' small rectangles with the heights parallel to the y axis.ie The length a to b is divided into n intervals.The area of each rectangle can be obtained by multiplying the height and the width.Height is the average of the values of f(x) at the two end points of the interval.Width is the length of the interval . Sum of the areas of all the rectangles gives us an approximation of the area of the area under f(x).&lt;br /&gt;
&lt;br /&gt;
Example: Suppose we need to compute the area under a curve described by some function,  x2 + 2x + 3 between say 10 and 20. &lt;br /&gt;
Then we can define a method poly for the Float class which returns the evaluation of the polynomial at that point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,end points between which the area is to be computed as the parameters &lt;br /&gt;
&amp;lt;pre&amp;gt;area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the   #points x=10 to x=20.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method  [http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_object.html#Object.method_missing method_missing]within the class definition provides this facility. This feature is not available in java.A class cast  [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Exception.html exception] is thrown whenever  a call to an undefined method is made. Ruby passes the name of the method called(which is unavailable) and the arguments passed to it as parameters to the method_missing  method . &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example : In the following example, class Duck defines 'quack' and 'method_missing' methods. When an object d of Duck is created and quack is invoked on it,it executes the quack of Duck(as expected). But when  'bark' is invoked on it, method_missing is invoked.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args) #handles calls to undefined methods&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark #bark is passed to method_missing since 'bark' method is not defined in Duck class.&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of an interesting usage of method_missing: “Roman method_missing”.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Roman &lt;br /&gt;
&lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    prev = nil #prev stores the previous value in roman string&lt;br /&gt;
    roman_string.to_s.upcase.split(//).reverse.inject(0) do |memo, digit|&lt;br /&gt;
     if digit_value = DIGITS[digit] #checks if the integer value is present in the hash&lt;br /&gt;
        if prev &amp;amp;&amp;amp; prev &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
        prev = digit_value&lt;br /&gt;
       end&lt;br /&gt;
      memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end    &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
 r=Roman.new&lt;br /&gt;
 r.XV&lt;br /&gt;
Output : =&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example we declare a class Roman containing a hash called DIGITS which maps  roman symbols with the corresponding integers. The class also contains the methods method_missing and roman_to_integer. When an object of Roman class( say r) is created and when we invoke method XV on r (ie r.XV) ,method_missing(:XV) is  invoked since the Roman class has no method named XV. Inside method_missing, the symbol is converted to &amp;quot;XV&amp;quot;(string) and  roman_to_str(&amp;quot;XV&amp;quot;) is called. In roman_to_str method, the string is converted to uppercase, split into an array and the array is reversed. The inject operator  then passes  each element (of the array)  and an accumulator value (memo)to the block.The initial value of  memo is 0.  For each element,if the previous digit_value exists and is greater than the current digit_value,the  current digit_value is added to memo. Else current digit_value is subtracted from memo.The result becomes the new value for memo. At the end of the iteration, the final value of memo is the integer value of the roman string.&lt;br /&gt;
&lt;br /&gt;
==Reflection in Java==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
* Which class does an object belong to?&lt;br /&gt;
* What is the description of a given class name?&lt;br /&gt;
* What are the fields in a given class?&lt;br /&gt;
* What is the type of a field?&lt;br /&gt;
* What are the methods in a class?&lt;br /&gt;
* What are the parameters of a method?&lt;br /&gt;
* What are the constructors of a given class? &lt;br /&gt;
* Constructing an object using a given constructor&lt;br /&gt;
* Invoking an object's method using such-and-such parameters&lt;br /&gt;
* Assigning a value to an object's field&lt;br /&gt;
* Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/package-summary.html java.lang.reflect].&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html 'Class']&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
class Fields-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Field.html 'Field'] class&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
methods-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Method.html 'Method'] class&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
constructors-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Constructor.html 'Constructor'] class&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
arrays-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Array.html 'Array'] class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52872</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52872"/>
		<updated>2011-10-20T03:09:50Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Reflection in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Introduction &lt;br /&gt;
&lt;br /&gt;
Here we mainly focus on the lecture 11 of CSC 517 which is Reflection in Ruby. We also cover additional information on Reflection in Java, advantages,disadvantages of Reflection&lt;br /&gt;
== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler  because it is done using the language mechanisms ie &amp;lt;object&amp;gt;''.methods'' gives the list of methods. This is in  contrast  to reflection in java which is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).Using Reflection,we can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 100&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum &lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
#instance_of? is a method of Object.returns true if the object is an instance of the #given class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Difference between Fixnum and Bignum in Ruby'''&lt;br /&gt;
&lt;br /&gt;
A Fixnum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Fixnum.html&amp;lt;/ref&amp;gt; stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Bignum.html&amp;lt;/ref&amp;gt; objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would  overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a [http://en.wikipedia.org/wiki/Primitive_data_type primitive] and  [http://en.wikipedia.org/wiki/Reference_type reference]with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.In contrast to Fixnum objects,  objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
Example 4:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Comparable.html '''Comparable'''] which is  an  ancestor of the [http://ruby-doc.org/core-1.9.2/String.html String] class is a [http://en.wikipedia.org/wiki/Mixin mixin] .It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Object.html '''Object'''] is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/BasicObject.html '''Basic Object'''] is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Kernel.html '''Kernel'''] is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Integer.html '''Integer''']is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Numeric.html '''Numeric'''] is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example.&lt;br /&gt;
&lt;br /&gt;
'''Note''': It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end &lt;br /&gt;
#this is not a good practice.Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say ,size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in Ruby,assignments physically copy references, but not the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the super class of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method(as a symbol) has to be passed  as a parameter to 'method'&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,a reference to the method upcase of the String class is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference(m) can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol. Thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol &lt;br /&gt;
332648                                    # yielded same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Numerical_integration'''Quadrature integration''']&lt;br /&gt;
Here we compute the area under a curve say f(x) between two points say 'a' and 'b' by dividing the region under the curve into 'n' small rectangles with the heights parallel to the y axis.ie The length a to b is divided into n intervals.The area of each rectangle can be obtained by multiplying the height and the width.Height is the average of the values of f(x) at the two end points of the interval.Width is the length of the interval . Sum of the areas of all the rectangles gives us an approximation of the area of the area under f(x).&lt;br /&gt;
&lt;br /&gt;
Example: Suppose we need to compute the area under a curve described by some function,  x2 + 2x + 3 between say 10 and 20. &lt;br /&gt;
Then we can define a method poly for the Float class which returns the evaluation of the polynomial at that point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,end points between which the area is to be computed as the parameters &lt;br /&gt;
&amp;lt;pre&amp;gt;area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the   #points x=10 to x=20.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method  [http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_object.html#Object.method_missing method_missing]within the class definition provides this facility. This feature is not available in java.A class cast  [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Exception.html exception] is thrown whenever  a call to an undefined method is made. Ruby passes the name of the method called(which is unavailable) and the arguments passed to it as parameters to the method_missing  method . &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example : In the following example, class Duck defines 'quack' and 'method_missing' methods. When an object d of Duck is created and quack is invoked on it,it executes the quack of Duck(as expected). But when  'bark' is invoked on it, method_missing is invoked.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args) #handles calls to undefined methods&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark #bark is passed to method_missing since 'bark' method is not defined in Duck class.&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of an interesting usage of method_missing: “Roman method_missing”.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Roman &lt;br /&gt;
&lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    prev = nil #prev stores the previous value in roman string&lt;br /&gt;
    roman_string.to_s.upcase.split(//).reverse.inject(0) do |memo, digit|&lt;br /&gt;
     if digit_value = DIGITS[digit] #checks if the integer value is present in the hash&lt;br /&gt;
        if prev &amp;amp;&amp;amp; prev &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
        prev = digit_value&lt;br /&gt;
       end&lt;br /&gt;
      memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end    &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
 r=Roman.new&lt;br /&gt;
 r.XV&lt;br /&gt;
Output : =&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example we declare a class Roman containing a hash called DIGITS which maps  roman symbols with the corresponding integers. The class also contains the methods method_missing and roman_to_integer. When an object of Roman class( say r) is created and when we invoke method XV on r (ie r.XV) ,method_missing(:XV) is  invoked since the Roman class has no method named XV. Inside method_missing, the symbol is converted to &amp;quot;XV&amp;quot;(string) and  roman_to_str(&amp;quot;XV&amp;quot;) is called. In roman_to_str method, the string is converted to uppercase, split into an array and the array is reversed. The inject operator  then passes  each element (of the array)  and an accumulator value (memo)to the block.The initial value of  memo is 0.  For each element,if the previous digit_value exists and is greater than the current digit_value,the  current digit_value is added to memo. Else current digit_value is subtracted from memo.The result becomes the new value for memo. At the end of the iteration, the final value of memo is the integer value of the roman string.&lt;br /&gt;
&lt;br /&gt;
==Reflection in Java==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
 * Which class does an object belong to?&lt;br /&gt;
 * What is the description of a given class name?&lt;br /&gt;
 * What are the fields in a given class?&lt;br /&gt;
 * What is the type of a field?&lt;br /&gt;
 * What are the methods in a class?&lt;br /&gt;
 * What are the parameters of a method?&lt;br /&gt;
 * What are the constructors of a given class? &lt;br /&gt;
 * Constructing an object using a given constructor&lt;br /&gt;
 * Invoking an object's method using such-and-such parameters&lt;br /&gt;
 * Assigning a value to an object's field&lt;br /&gt;
 * Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/package-summary.html java.lang.reflect].&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html 'Class'] &lt;br /&gt;
class Fields-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Field.html 'Field'] class&lt;br /&gt;
methods-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Method.html 'Method'] class&lt;br /&gt;
constructors-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Constructor.html 'Constructor'] class&lt;br /&gt;
arrays-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Array.html 'Array'] class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52870</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52870"/>
		<updated>2011-10-20T03:07:45Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Reflection in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Introduction &lt;br /&gt;
&lt;br /&gt;
Here we mainly focus on the lecture 11 of CSC 517 which is Reflection in Ruby. We also cover additional information on Reflection in Java, advantages,disadvantages of Reflection&lt;br /&gt;
== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler  because it is done using the language mechanisms ie &amp;lt;object&amp;gt;''.methods'' gives the list of methods. This is in  contrast  to reflection in java which is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).Using Reflection,we can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 100&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum &lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
#instance_of? is a method of Object.returns true if the object is an instance of the #given class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Difference between Fixnum and Bignum in Ruby'''&lt;br /&gt;
&lt;br /&gt;
A Fixnum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Fixnum.html&amp;lt;/ref&amp;gt; stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Bignum.html&amp;lt;/ref&amp;gt; objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would  overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a [http://en.wikipedia.org/wiki/Primitive_data_type primitive] and  [http://en.wikipedia.org/wiki/Reference_type reference]with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.In contrast to Fixnum objects,  objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
Example 4:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Comparable.html '''Comparable'''] which is  an  ancestor of the [http://ruby-doc.org/core-1.9.2/String.html String] class is a [http://en.wikipedia.org/wiki/Mixin mixin] .It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Object.html '''Object'''] is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/BasicObject.html '''Basic Object'''] is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Kernel.html '''Kernel'''] is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Integer.html '''Integer''']is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Numeric.html '''Numeric'''] is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example.&lt;br /&gt;
&lt;br /&gt;
'''Note''': It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end &lt;br /&gt;
#this is not a good practice.Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say ,size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in Ruby,assignments physically copy references, but not the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the super class of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method(as a symbol) has to be passed  as a parameter to 'method'&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,a reference to the method upcase of the String class is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference(m) can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol. Thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol &lt;br /&gt;
332648                                    # yielded same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Numerical_integration'''Quadrature integration''']&lt;br /&gt;
Here we compute the area under a curve say f(x) between two points say 'a' and 'b' by dividing the region under the curve into 'n' small rectangles with the heights parallel to the y axis.ie The length a to b is divided into n intervals.The area of each rectangle can be obtained by multiplying the height and the width.Height is the average of the values of f(x) at the two end points of the interval.Width is the length of the interval . Sum of the areas of all the rectangles gives us an approximation of the area of the area under f(x).&lt;br /&gt;
&lt;br /&gt;
Example: Suppose we need to compute the area under a curve described by some function,  x2 + 2x + 3 between say 10 and 20. &lt;br /&gt;
Then we can define a method poly for the Float class which returns the evaluation of the polynomial at that point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,end points between which the area is to be computed as the parameters &lt;br /&gt;
&amp;lt;pre&amp;gt;area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the   #points x=10 to x=20.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method  [http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_object.html#Object.method_missing method_missing]within the class definition provides this facility. This feature is not available in java.A class cast  [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Exception.html exception] is thrown whenever  a call to an undefined method is made. Ruby passes the name of the method called(which is unavailable) and the arguments passed to it as parameters to the method_missing  method . &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example : In the following example, class Duck defines 'quack' and 'method_missing' methods. When an object d of Duck is created and quack is invoked on it,it executes the quack of Duck(as expected). But when  'bark' is invoked on it, method_missing is invoked.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args) #handles calls to undefined methods&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark #bark is passed to method_missing since 'bark' method is not defined in Duck class.&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of an interesting usage of method_missing: “Roman method_missing”.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Roman &lt;br /&gt;
&lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    prev = nil #prev stores the previous value in roman string&lt;br /&gt;
    roman_string.to_s.upcase.split(//).reverse.inject(0) do |memo, digit|&lt;br /&gt;
     if digit_value = DIGITS[digit] #checks if the integer value is present in the hash&lt;br /&gt;
        if prev &amp;amp;&amp;amp; prev &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
        prev = digit_value&lt;br /&gt;
       end&lt;br /&gt;
      memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end    &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
 r=Roman.new&lt;br /&gt;
 r.XV&lt;br /&gt;
Output : =&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example we declare a class Roman containing a hash called DIGITS which maps  roman symbols with the corresponding integers. The class also contains the methods method_missing and roman_to_integer. When an object of Roman class( say r) is created and when we invoke method XV on r (ie r.XV) ,method_missing(:XV) is  invoked since the Roman class has no method named XV. Inside method_missing, the symbol is converted to &amp;quot;XV&amp;quot;(string) and  roman_to_str(&amp;quot;XV&amp;quot;) is called. In roman_to_str method, the string is converted to uppercase, split into an array and the array is reversed. The inject operator  then passes  each element (of the array)  and an accumulator value (memo)to the block.The initial value of  memo is 0.  For each element,if the previous digit_value exists and is greater than the current digit_value,the  current digit_value is added to memo. Else current digit_value is subtracted from memo.The result becomes the new value for memo. At the end of the iteration, the final value of memo is the integer value of the roman string.&lt;br /&gt;
&lt;br /&gt;
==Reflection in Java==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *Which class does an object belong to?&lt;br /&gt;
 *What is the description of a given class name?&lt;br /&gt;
 *What are the fields in a given class?&lt;br /&gt;
 *What is the type of a field?&lt;br /&gt;
 *What are the methods in a class?&lt;br /&gt;
 *What are the parameters of a method?&lt;br /&gt;
 *What are the constructors of a given class? &lt;br /&gt;
 *Constructing an object using a given constructor&lt;br /&gt;
 *Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *Assigning a value to an object's field&lt;br /&gt;
 *Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/package-summary.html java.lang.reflect].&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html 'Class'] &lt;br /&gt;
class Fields-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Field.html 'Field'] class&lt;br /&gt;
methods-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Method.html 'Method'] class&lt;br /&gt;
constructors-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Constructor.html 'Constructor'] class&lt;br /&gt;
arrays-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Array.html 'Array'] class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52864</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52864"/>
		<updated>2011-10-20T03:03:41Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Reflection in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Introduction &lt;br /&gt;
&lt;br /&gt;
Here we mainly focus on the lecture 11 of CSC 517 which is Reflection in Ruby. We also cover additional information on Reflection in Java, advantages,disadvantages of Reflection&lt;br /&gt;
== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler  because it is done using the language mechanisms ie &amp;lt;object&amp;gt;''.methods'' gives the list of methods. This is in  contrast  to reflection in java which is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).Using Reflection,we can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 100&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum &lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
#instance_of? is a method of Object.returns true if the object is an instance of the #given class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Difference between Fixnum and Bignum in Ruby'''&lt;br /&gt;
&lt;br /&gt;
A Fixnum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Fixnum.html&amp;lt;/ref&amp;gt; stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Bignum.html&amp;lt;/ref&amp;gt; objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would  overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a [http://en.wikipedia.org/wiki/Primitive_data_type primitive] and  [http://en.wikipedia.org/wiki/Reference_type reference]with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.In contrast to Fixnum objects,  objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
Example 4:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Comparable.html '''Comparable'''] which is  an  ancestor of the [http://ruby-doc.org/core-1.9.2/String.html String] class is a [http://en.wikipedia.org/wiki/Mixin mixin] .It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Object.html '''Object'''] is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/BasicObject.html '''Basic Object'''] is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Kernel.html '''Kernel'''] is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Integer.html '''Integer''']is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Numeric.html '''Numeric'''] is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example.&lt;br /&gt;
&lt;br /&gt;
'''Note''': It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end &lt;br /&gt;
#this is not a good practice.Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say ,size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in Ruby,assignments physically copy references, but not the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the super class of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method(as a symbol) has to be passed  as a parameter to 'method'&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,a reference to the method upcase of the String class is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference(m) can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol. Thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol &lt;br /&gt;
332648                                    # yielded same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Numerical_integration'''Quadrature integration''']&lt;br /&gt;
Here we compute the area under a curve say f(x) between two points say 'a' and 'b' by dividing the region under the curve into 'n' small rectangles with the heights parallel to the y axis.ie The length a to b is divided into n intervals.The area of each rectangle can be obtained by multiplying the height and the width.Height is the average of the values of f(x) at the two end points of the interval.Width is the length of the interval . Sum of the areas of all the rectangles gives us an approximation of the area of the area under f(x).&lt;br /&gt;
&lt;br /&gt;
Example: Suppose we need to compute the area under a curve described by some function,  x2 + 2x + 3 between say 10 and 20. &lt;br /&gt;
Then we can define a method poly for the Float class which returns the evaluation of the polynomial at that point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,end points between which the area is to be computed as the parameters &lt;br /&gt;
&amp;lt;pre&amp;gt;area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the   #points x=10 to x=20.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method  [http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_object.html#Object.method_missing method_missing]within the class definition provides this facility. This feature is not available in java.A class cast  [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Exception.html exception] is thrown whenever  a call to an undefined method is made. Ruby passes the name of the method called(which is unavailable) and the arguments passed to it as parameters to the method_missing  method . &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example : In the following example, class Duck defines 'quack' and 'method_missing' methods. When an object d of Duck is created and quack is invoked on it,it executes the quack of Duck(as expected). But when  'bark' is invoked on it, method_missing is invoked.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args) #handles calls to undefined methods&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark #bark is passed to method_missing since 'bark' method is not defined in Duck class.&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of an interesting usage of method_missing: “Roman method_missing”.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Roman &lt;br /&gt;
&lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    prev = nil #prev stores the previous value in roman string&lt;br /&gt;
    roman_string.to_s.upcase.split(//).reverse.inject(0) do |memo, digit|&lt;br /&gt;
     if digit_value = DIGITS[digit] #checks if the integer value is present in the hash&lt;br /&gt;
        if prev &amp;amp;&amp;amp; prev &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
        prev = digit_value&lt;br /&gt;
       end&lt;br /&gt;
      memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end    &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
 r=Roman.new&lt;br /&gt;
 r.XV&lt;br /&gt;
Output : =&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example we declare a class Roman containing a hash called DIGITS which maps  roman symbols with the corresponding integers. The class also contains the methods method_missing and roman_to_integer. When an object of Roman class( say r) is created and when we invoke method XV on r (ie r.XV) ,method_missing(:XV) is  invoked since the Roman class has no method named XV. Inside method_missing, the symbol is converted to &amp;quot;XV&amp;quot;(string) and  roman_to_str(&amp;quot;XV&amp;quot;) is called. In roman_to_str method, the string is converted to uppercase, split into an array and the array is reversed. The inject operator  then passes  each element (of the array)  and an accumulator value (memo)to the block.The initial value of  memo is 0.  For each element,if the previous digit_value exists and is greater than the current digit_value,the  current digit_value is added to memo. Else current digit_value is subtracted from memo.The result becomes the new value for memo. At the end of the iteration, the final value of memo is the integer value of the roman string.&lt;br /&gt;
&lt;br /&gt;
==Reflection in Java==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/package-summary.html java.lang.reflect].&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html 'Class'] &lt;br /&gt;
class Fields-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Field.html 'Field'] class&lt;br /&gt;
methods-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Method.html 'Method'] class&lt;br /&gt;
constructors-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Constructor.html 'Constructor'] class&lt;br /&gt;
arrays-[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Array.html 'Array'] class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52858</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52858"/>
		<updated>2011-10-20T02:55:02Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Applications of Reflection in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Introduction &lt;br /&gt;
&lt;br /&gt;
Here we mainly focus on the lecture 11 of CSC 517 which is Reflection in Ruby. We also cover additional information on Reflection in Java, advantages,disadvantages of Reflection&lt;br /&gt;
== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler  because it is done using the language mechanisms ie &amp;lt;object&amp;gt;''.methods'' gives the list of methods. This is in  contrast  to reflection in java which is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).Using Reflection,we can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 100&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum &lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
#instance_of? is a method of Object.returns true if the object is an instance of the #given class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Difference between Fixnum and Bignum in Ruby'''&lt;br /&gt;
&lt;br /&gt;
A Fixnum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Fixnum.html&amp;lt;/ref&amp;gt; stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Bignum.html&amp;lt;/ref&amp;gt; objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would  overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a [http://en.wikipedia.org/wiki/Primitive_data_type primitive] and  [http://en.wikipedia.org/wiki/Reference_type reference]with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.In contrast to Fixnum objects,  objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
Example 4:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Comparable.html '''Comparable'''] which is  an  ancestor of the [http://ruby-doc.org/core-1.9.2/String.html String] class is a [http://en.wikipedia.org/wiki/Mixin mixin] .It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Object.html '''Object'''] is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/BasicObject.html '''Basic Object'''] is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Kernel.html '''Kernel'''] is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Integer.html '''Integer''']is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Numeric.html '''Numeric'''] is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example.&lt;br /&gt;
&lt;br /&gt;
'''Note''': It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end &lt;br /&gt;
#this is not a good practice.Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say ,size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in Ruby,assignments physically copy references, but not the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the super class of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method(as a symbol) has to be passed  as a parameter to 'method'&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,a reference to the method upcase of the String class is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference(m) can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol. Thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol &lt;br /&gt;
332648                                    # yielded same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Numerical_integration'''Quadrature integration''']&lt;br /&gt;
Here we compute the area under a curve say f(x) between two points say 'a' and 'b' by dividing the region under the curve into 'n' small rectangles with the heights parallel to the y axis.ie The length a to b is divided into n intervals.The area of each rectangle can be obtained by multiplying the height and the width.Height is the average of the values of f(x) at the two end points of the interval.Width is the length of the interval . Sum of the areas of all the rectangles gives us an approximation of the area of the area under f(x).&lt;br /&gt;
&lt;br /&gt;
Example: Suppose we need to compute the area under a curve described by some function,  x2 + 2x + 3 between say 10 and 20. &lt;br /&gt;
Then we can define a method poly for the Float class which returns the evaluation of the polynomial at that point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,end points between which the area is to be computed as the parameters &lt;br /&gt;
&amp;lt;pre&amp;gt;area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the   #points x=10 to x=20.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method  [http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_object.html#Object.method_missing method_missing]within the class definition provides this facility. This feature is not available in java.A class cast  [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Exception.html exception] is thrown whenever  a call to an undefined method is made. Ruby passes the name of the method called(which is unavailable) and the arguments passed to it as parameters to the method_missing  method . &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example : In the following example, class Duck defines 'quack' and 'method_missing' methods. When an object d of Duck is created and quack is invoked on it,it executes the quack of Duck(as expected). But when  'bark' is invoked on it, method_missing is invoked.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args) #handles calls to undefined methods&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark #bark is passed to method_missing since 'bark' method is not defined in Duck class.&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of an interesting usage of method_missing: “Roman method_missing”.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Roman &lt;br /&gt;
&lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    prev = nil #prev stores the previous value in roman string&lt;br /&gt;
    roman_string.to_s.upcase.split(//).reverse.inject(0) do |memo, digit|&lt;br /&gt;
     if digit_value = DIGITS[digit] #checks if the integer value is present in the hash&lt;br /&gt;
        if prev &amp;amp;&amp;amp; prev &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
        prev = digit_value&lt;br /&gt;
       end&lt;br /&gt;
      memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end    &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
 r=Roman.new&lt;br /&gt;
 r.XV&lt;br /&gt;
Output : =&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example we declare a class Roman containing a hash called DIGITS which maps  roman symbols with the corresponding integers. The class also contains the methods method_missing and roman_to_integer. When an object of Roman class( say r) is created and when we invoke method XV on r (ie r.XV) ,method_missing(:XV) is  invoked since the Roman class has no method named XV. Inside method_missing, the symbol is converted to &amp;quot;XV&amp;quot;(string) and  roman_to_str(&amp;quot;XV&amp;quot;) is called. In roman_to_str method, the string is converted to uppercase, split into an array and the array is reversed. The inject operator  then passes  each element (of the array)  and an accumulator value (memo)to the block.The initial value of  memo is 0.  For each element,if the previous digit_value exists and is greater than the current digit_value,the  current digit_value is added to memo. Else current digit_value is subtracted from memo.The result becomes the new value for memo. At the end of the iteration, the final value of memo is the integer value of the roman string.&lt;br /&gt;
&lt;br /&gt;
==Reflection in Java==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52855</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52855"/>
		<updated>2011-10-20T02:52:01Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Advantages of Reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Introduction &lt;br /&gt;
&lt;br /&gt;
Here we mainly focus on the lecture 11 of CSC 517 which is Reflection in Ruby. We also cover additional information on Reflection in Java, advantages,disadvantages of Reflection&lt;br /&gt;
== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler  because it is done using the language mechanisms ie &amp;lt;object&amp;gt;''.methods'' gives the list of methods. This is in  contrast  to reflection in java which is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).Using Reflection,we can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 100&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum &lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
#instance_of? is a method of Object.returns true if the object is an instance of the #given class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Difference between Fixnum and Bignum in Ruby'''&lt;br /&gt;
&lt;br /&gt;
A Fixnum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Fixnum.html&amp;lt;/ref&amp;gt; stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Bignum.html&amp;lt;/ref&amp;gt; objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would  overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a [http://en.wikipedia.org/wiki/Primitive_data_type primitive] and  [http://en.wikipedia.org/wiki/Reference_type reference]with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.In contrast to Fixnum objects,  objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
Example 4:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Comparable.html '''Comparable'''] which is  an  ancestor of the [http://ruby-doc.org/core-1.9.2/String.html String] class is a [http://en.wikipedia.org/wiki/Mixin mixin] .It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Object.html '''Object'''] is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/BasicObject.html '''Basic Object'''] is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Kernel.html '''Kernel'''] is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Integer.html '''Integer''']is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Numeric.html '''Numeric'''] is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example.&lt;br /&gt;
&lt;br /&gt;
'''Note''': It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end &lt;br /&gt;
#this is not a good practice.Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say ,size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in Ruby,assignments physically copy references, but not the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the super class of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method(as a symbol) has to be passed  as a parameter to 'method'&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,a reference to the method upcase of the String class is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference(m) can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol. Thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol &lt;br /&gt;
332648                                    # yielded same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Numerical_integration'''Quadrature integration''']&lt;br /&gt;
Here we compute the area under a curve say f(x) between two points say 'a' and 'b' by dividing the region under the curve into 'n' small rectangles with the heights parallel to the y axis.ie The length a to b is divided into n intervals.The area of each rectangle can be obtained by multiplying the height and the width.Height is the average of the values of f(x) at the two end points of the interval.Width is the length of the interval . Sum of the areas of all the rectangles gives us an approximation of the area of the area under f(x).&lt;br /&gt;
&lt;br /&gt;
Example: Suppose we need to compute the area under a curve described by some function,  x2 + 2x + 3 between say 10 and 20. &lt;br /&gt;
Then we can define a method poly for the Float class which returns the evaluation of the polynomial at that point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,end points between which the area is to be computed as the parameters &lt;br /&gt;
&amp;lt;pre&amp;gt;area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the   #points x=10 to x=20.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method  [http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_object.html#Object.method_missing method_missing]within the class definition provides this facility. This feature is not available in java.A class cast  [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Exception.html exception] is thrown whenever  a call to an undefined method is made. Ruby passes the name of the method called(which is unavailable) and the arguments passed to it as parameters to the method_missing  method . &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example : In the following example, class Duck defines 'quack' and 'method_missing' methods. When an object d of Duck is created and quack is invoked on it,it executes the quack of Duck(as expected). But when  'bark' is invoked on it, method_missing is invoked.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args) #handles calls to undefined methods&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark #bark is passed to method_missing since 'bark' method is not defined in Duck class.&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of an interesting usage of method_missing: “Roman method_missing”.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Roman &lt;br /&gt;
&lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    prev = nil #prev stores the previous value in roman string&lt;br /&gt;
    roman_string.to_s.upcase.split(//).reverse.inject(0) do |memo, digit|&lt;br /&gt;
     if digit_value = DIGITS[digit] #checks if the integer value is present in the hash&lt;br /&gt;
        if prev &amp;amp;&amp;amp; prev &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
        prev = digit_value&lt;br /&gt;
       end&lt;br /&gt;
      memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end    &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
 r=Roman.new&lt;br /&gt;
 r.XV&lt;br /&gt;
Output : =&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example we declare a class Roman containing a hash called DIGITS which maps  roman symbols with the corresponding integers. The class also contains the methods method_missing and roman_to_integer. When an object of Roman class( say r) is created and when we invoke method XV on r (ie r.XV) ,method_missing(:XV) is  invoked since the Roman class has no method named XV. Inside method_missing, the symbol is converted to &amp;quot;XV&amp;quot;(string) and  roman_to_str(&amp;quot;XV&amp;quot;) is called. In roman_to_str method, the string is converted to uppercase, split into an array and the array is reversed. The inject operator  then passes  each element (of the array)  and an accumulator value (memo)to the block.The initial value of  memo is 0.  For each element,if the previous digit_value exists and is greater than the current digit_value,the  current digit_value is added to memo. Else current digit_value is subtracted from memo.The result becomes the new value for memo. At the end of the iteration, the final value of memo is the integer value of the roman string.&lt;br /&gt;
&lt;br /&gt;
==Reflection in Java==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection&amp;lt;ref&amp;gt;http://java.sys-con.com/node/36688&amp;lt;ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52833</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52833"/>
		<updated>2011-10-20T02:06:18Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Intercepting calls to undefined methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Introduction &lt;br /&gt;
&lt;br /&gt;
Here we mainly focus on the lecture 11 of CSC 517 which is Reflection in Ruby. We also cover additional information on Reflection in Java, advantages,disadvantages of Reflection&lt;br /&gt;
== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler  because it is done using the language mechanisms ie &amp;lt;object&amp;gt;''.methods'' gives the list of methods. This is in  contrast  to reflection in java which is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).Using Reflection,we can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 100&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum &lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
#instance_of? is a method of Object.returns true if the object is an instance of the #given class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Difference between Fixnum and Bignum in Ruby'''&lt;br /&gt;
&lt;br /&gt;
A Fixnum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Fixnum.html&amp;lt;/ref&amp;gt; stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Bignum.html&amp;lt;/ref&amp;gt; objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would  overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a [http://en.wikipedia.org/wiki/Primitive_data_type primitive] and  [http://en.wikipedia.org/wiki/Reference_type reference]with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.In contrast to Fixnum objects,  objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
Example 4:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Comparable.html '''Comparable'''] which is  an  ancestor of the [http://ruby-doc.org/core-1.9.2/String.html String] class is a [http://en.wikipedia.org/wiki/Mixin mixin] .It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Object.html '''Object'''] is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/BasicObject.html '''Basic Object'''] is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Kernel.html '''Kernel'''] is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Integer.html '''Integer''']is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Numeric.html '''Numeric'''] is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example.&lt;br /&gt;
&lt;br /&gt;
'''Note''': It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end &lt;br /&gt;
#this is not a good practice.Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say ,size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in Ruby,assignments physically copy references, but not the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the super class of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method(as a symbol) has to be passed  as a parameter to 'method'&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,a reference to the method upcase of the String class is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference(m) can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol. Thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol &lt;br /&gt;
332648                                    # yielded same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Numerical_integration'''Quadrature integration''']&lt;br /&gt;
Here we compute the area under a curve say f(x) between two points say 'a' and 'b' by dividing the region under the curve into 'n' small rectangles with the heights parallel to the y axis.ie The length a to b is divided into n intervals.The area of each rectangle can be obtained by multiplying the height and the width.Height is the average of the values of f(x) at the two end points of the interval.Width is the length of the interval . Sum of the areas of all the rectangles gives us an approximation of the area of the area under f(x).&lt;br /&gt;
&lt;br /&gt;
Example: Suppose we need to compute the area under a curve described by some function,  x2 + 2x + 3 between say 10 and 20. &lt;br /&gt;
Then we can define a method poly for the Float class which returns the evaluation of the polynomial at that point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,end points between which the area is to be computed as the parameters &lt;br /&gt;
&amp;lt;pre&amp;gt;area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the   #points x=10 to x=20.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method  [http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_object.html#Object.method_missing method_missing]within the class definition provides this facility. This feature is not available in java.A class cast  [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Exception.html exception] is thrown whenever  a call to an undefined method is made. Ruby passes the name of the method called(which is unavailable) and the arguments passed to it as parameters to the method_missing  method . &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example : In the following example, class Duck defines 'quack' and 'method_missing' methods. When an object d of Duck is created and quack is invoked on it,it executes the quack of Duck(as expected). But when  'bark' is invoked on it, method_missing is invoked.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args) #handles calls to undefined methods&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark #bark is passed to method_missing since 'bark' method is not defined in Duck class.&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of an interesting usage of method_missing: “Roman method_missing”.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Roman &lt;br /&gt;
&lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    prev = nil #prev stores the previous value in roman string&lt;br /&gt;
    roman_string.to_s.upcase.split(//).reverse.inject(0) do |memo, digit|&lt;br /&gt;
     if digit_value = DIGITS[digit] #checks if the integer value is present in the hash&lt;br /&gt;
        if prev &amp;amp;&amp;amp; prev &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
        prev = digit_value&lt;br /&gt;
       end&lt;br /&gt;
      memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end    &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
 r=Roman.new&lt;br /&gt;
 r.XV&lt;br /&gt;
Output : =&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example we declare a class Roman containing a hash called DIGITS which maps  roman symbols with the corresponding integers. The class also contains the methods method_missing and roman_to_integer. When an object of Roman class( say r) is created and when we invoke method XV on r (ie r.XV) ,method_missing(:XV) is  invoked since the Roman class has no method named XV. Inside method_missing, the symbol is converted to &amp;quot;XV&amp;quot;(string) and  roman_to_str(&amp;quot;XV&amp;quot;) is called. In roman_to_str method, the string is converted to uppercase, split into an array and the array is reversed. The inject operator  then passes  each element (of the array)  and an accumulator value (memo)to the block.The initial value of  memo is 0.  For each element,if the previous digit_value exists and is greater than the current digit_value,the  current digit_value is added to memo. Else current digit_value is subtracted from memo.The result becomes the new value for memo. At the end of the iteration, the final value of memo is the integer value of the roman string.&lt;br /&gt;
&lt;br /&gt;
==Reflection in Java==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52830</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52830"/>
		<updated>2011-10-20T01:57:49Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Introduction &lt;br /&gt;
&lt;br /&gt;
Here we mainly focus on the lecture 11 of CSC 517 which is Reflection in Ruby. We also cover additional information on Reflection in Java, advantages,disadvantages of Reflection&lt;br /&gt;
== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler  because it is done using the language mechanisms ie &amp;lt;object&amp;gt;''.methods'' gives the list of methods. This is in  contrast  to reflection in java which is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).Using Reflection,we can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
=&amp;gt;Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 100&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer #&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum &lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
#instance_of? is a method of Object.returns true if the object is an instance of the #given class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Difference between Fixnum and Bignum in Ruby'''&lt;br /&gt;
&lt;br /&gt;
A Fixnum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Fixnum.html&amp;lt;/ref&amp;gt; stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum&amp;lt;ref&amp;gt;http://ruby-doc.org/core-1.9.2/Bignum.html&amp;lt;/ref&amp;gt; objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would  overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a [http://en.wikipedia.org/wiki/Primitive_data_type primitive] and  [http://en.wikipedia.org/wiki/Reference_type reference]with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.In contrast to Fixnum objects,  objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
Example 4:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Comparable.html '''Comparable'''] which is  an  ancestor of the [http://ruby-doc.org/core-1.9.2/String.html String] class is a [http://en.wikipedia.org/wiki/Mixin mixin] .It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Object.html '''Object'''] is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/BasicObject.html '''Basic Object'''] is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Kernel.html '''Kernel'''] is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Integer.html '''Integer''']is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.2/Numeric.html '''Numeric'''] is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example.&lt;br /&gt;
&lt;br /&gt;
'''Note''': It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end &lt;br /&gt;
#this is not a good practice.Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say ,size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in Ruby,assignments physically copy references, but not the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the super class of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method(as a symbol) has to be passed  as a parameter to 'method'&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,a reference to the method upcase of the String class is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference(m) can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol. Thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol &lt;br /&gt;
332648                                    # yielded same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Numerical_integration'''Quadrature integration''']&lt;br /&gt;
Here we compute the area under a curve say f(x) between two points say 'a' and 'b' by dividing the region under the curve into 'n' small rectangles with the heights parallel to the y axis.ie The length a to b is divided into n intervals.The area of each rectangle can be obtained by multiplying the height and the width.Height is the average of the values of f(x) at the two end points of the interval.Width is the length of the interval . Sum of the areas of all the rectangles gives us an approximation of the area of the area under f(x).&lt;br /&gt;
&lt;br /&gt;
Example: Suppose we need to compute the area under a curve described by some function,  x2 + 2x + 3 between say 10 and 20. &lt;br /&gt;
Then we can define a method poly for the Float class which returns the evaluation of the polynomial at that point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,end points between which the area is to be computed as the parameters &lt;br /&gt;
&amp;lt;pre&amp;gt;area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the   #points x=10 to x=20.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility. This feature is not available in java.A class cast exception is thrown whenever  a call to an undefined method is made. Ruby passes the name of the method called(which is unavailable) and the arguments passed to it as parameters to the method_missing  method . &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example : In the following example, class Duck defines 'quack' and 'method_missing' methods. When an object d of Duck is created and quack is invoked on it,it executes the quack of Duck(as expected). But when  'bark' is invoked on it, method_missing is invoked.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args) #handles calls to undefined methods&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark #bark is passed to method_missing since 'bark' method is not defined in Duck class.&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of an interesting usage of method_missing: “Roman method_missing”.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Roman &lt;br /&gt;
&lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    prev = nil #prev stores the previous value in roman string&lt;br /&gt;
    roman_string.to_s.upcase.split(//).reverse.inject(0) do |memo, digit|&lt;br /&gt;
     if digit_value = DIGITS[digit] #checks if the integer value is present in the hash&lt;br /&gt;
        if prev &amp;amp;&amp;amp; prev &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
        prev = digit_value&lt;br /&gt;
       end&lt;br /&gt;
      memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end    &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
 r=Roman.new&lt;br /&gt;
 r.XV&lt;br /&gt;
Output : =&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example we declare a class Roman containing a hash called DIGITS which maps  roman symbols with the corresponding integers. The class also contains the methods method_missing and roman_to_integer. When an object of Roman class( say r) is created and when we invoke method XV on r (ie r.XV) ,method_missing(:XV) is  invoked since the Roman class has no method named XV. Inside method_missing, the symbol is converted to &amp;quot;XV&amp;quot;(string) and  roman_to_str(&amp;quot;XV&amp;quot;) is called. In roman_to_str method, the string is converted to uppercase, split into an array and the array is reversed. The inject operator  then passes  each element (of the array)  and an accumulator value (memo)to the block.The initial value of  memo is 0.  For each element,if the previous digit_value exists and is greater than the current digit_value,the  current digit_value is added to memo. Else current digit_value is subtracted from memo.The result becomes the new value for memo. At the end of the iteration, the final value of memo is the integer value of the roman string.&lt;br /&gt;
&lt;br /&gt;
==Reflection in Java==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52794</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52794"/>
		<updated>2011-10-20T00:31:40Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Introduction &lt;br /&gt;
&lt;br /&gt;
Here we mainly focus on the lecture 11 of CSC 517 which is Reflection in Ruby. We also cover additional information on Reflection in Java, advantages,disadvantages of Reflection&lt;br /&gt;
== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3: Let us now consider the example as discussed in the class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Difference between Fixnum and Bignum in Ruby'''&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
Example 4:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example.&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in Ruby,assignments physically copy references, but not the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,the reference to the method upcase of the string is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol and thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
Here we compute the area under a curve say f(x) between two points say 'a' and 'b' by dividing the region under the curve into small rectangles with the lengths parallel to the y axis.The area of the rectangle can be obtained by multiplying the height and the breadth.Height is the average of the values of f(x) at the two vertices.Breadth is the distance between the two vertices. Sum of the areas of all the rectangles gives us an approximation of the area of the area under f(x).&lt;br /&gt;
&lt;br /&gt;
Example: Suppose we need to compute the area under a curve described by some function,  x2 + 2x + 3 between say 10 and 20. &lt;br /&gt;
Then we can define&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,end points between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the points x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
To do this, we pick an interval, say (20−10)/100 = 1/10, and evaluate the polynomial for values between 10 and 20 at intervals of 1/10, e.g., 10, 10.1, 10.2, … , 19.9, 20.0. We take the value of the polynomial at 2 successive points, average these two values(height of rectangle), and multiply by 1/10 (the distance between the two points ie the width).  After we do this for all 100 intervals and add up the areas, we obtain an approximation of the area under the curve.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility. This feature is not available in java as a class cast exception is thrown whenever an a call to an undefined method is made. Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of an interesting usage of method_missing: “Roman method_missing”.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Roman &lt;br /&gt;
&lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    prev = nil #prev stores the previous value in roman string&lt;br /&gt;
    roman_string.to_s.upcase.split(//).reverse.inject(0) do |memo, digit|&lt;br /&gt;
     if digit_value = DIGITS[digit] #checks if the integer value is present in the hash&lt;br /&gt;
        if prev &amp;amp;&amp;amp; prev &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
        prev = digit_value&lt;br /&gt;
       end&lt;br /&gt;
      memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end    &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
 r=Roman.new&lt;br /&gt;
 r.XV&lt;br /&gt;
Output : =&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example we declare a class Roman containing a hash called DIGITS which maps  roman symbols with the corresponding integers. The class also contains the methods method_missing and roman_to_integer. When an object of Roman class( say r) is created and when we do r.XV ,method_missing(:XV) is  invoked since the Roman class has no method named XV. Inside method_missing, the symbol is converted to &amp;quot;XV&amp;quot;(string) and  roman_to_str(&amp;quot;XV&amp;quot;) is called. In roman_to_str method, the string is converted to uppercase,split into an array and the array is reversed. The inject operator  then passes  each element in the array  and an accumulator value (memo)to the block.The initial value of  memo  For each element,if the previous digit_value exists and is greater than the current digit_value,the  current digit_value is added to memo. Else current digit_value is subtracted from memo.The result becomes the new value for memo. At the end of the iteration, the final value of memo is the integer value of the roman string.&lt;br /&gt;
&lt;br /&gt;
==Reflection in Java==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52770</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52770"/>
		<updated>2011-10-19T23:52:45Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Intercepting calls to undefined methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3: Let us now consider the example as discussed in the class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Difference between Fixnum and Bignum in Ruby'''&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
Example 4:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example.&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in Ruby,assignments physically copy references, but not the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,the reference to the method upcase of the string is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol and thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
Here we compute the area under a curve say f(x) between two points say 'a' and 'b' by dividing the region under the curve into small rectangles with the lengths parallel to the y axis.The area of the rectangle can be obtained by multiplying the height and the breadth.Height is the average of the values of f(x) at the two vertices.Breadth is the distance between the two vertices. Sum of the areas of all the rectangles gives us an approximation of the area of the area under f(x).&lt;br /&gt;
&lt;br /&gt;
e.g.Suppose we need to compute the area under a curve described by some function,  x2 + 2x + 3 between say 10 and 20. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,end points between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the points x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
To do this, we pick an interval, say (20−10)/100 = 1/10, and evaluate the polynomial for values between 10 and 20 at intervals of 1/10, e.g., 10, 10.1, 10.2, … , 19.9, 20.0. We take the value of the polynomial at 2 successive points, average these two values(height of rectangle), and multiply by 1/10 (the distance between the two points ie the width).  After we do this for all 100 intervals and add up the areas, we obtain an approximation of the area under the curve.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility. This feature is not available in java as a class cast exception is thrown whenever an a call to an undefined method is made. Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
'''&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Roman &lt;br /&gt;
&lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    prev = nil #prev stores the previous value in roman string&lt;br /&gt;
    roman_string.to_s.upcase.split(//).reverse.inject(0) do |memo, digit|&lt;br /&gt;
      if digit_value = DIGITS[digit] #checks if the integer value is present in the hash&lt;br /&gt;
        if prev &amp;amp;&amp;amp; prev &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
        prev = digit_value&lt;br /&gt;
      end&lt;br /&gt;
      memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end    &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
 r=Roman.new&lt;br /&gt;
 r.XV&lt;br /&gt;
Output : =&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example we declare a class Roman containing a hash called DIGITS which maps  roman symbols with the corresponding integers. The class also contains the methods method_missing and roman_to_integer. When an object of Roman class( say r) is created and when we do r.XV ,method_missing(:XV) is  invoked since the Roman class has no method named XV. Inside method_missing, the symbol is converted to &amp;quot;XV&amp;quot;(string) and  roman_to_str(&amp;quot;XV&amp;quot;) is called. In roman_to_str method, the string is converted to uppercase,split into an array and the array is reversed. The inject operator  then passes  each element in the array  and an accumulator value (memo)to the block.The initial value of  memo  For each element,if the previous digit_value exists and is greater than the current digit_value,the  current digit_value is added to memo. Else current digit_value is subtracted from memo.The result becomes the new value for memo. At the end of the iteration, the final value of memo is the integer value of the roman string.&lt;br /&gt;
&lt;br /&gt;
==Reflection in Java==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52769</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52769"/>
		<updated>2011-10-19T23:51:51Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Applications of passing methods as parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3: Let us now consider the example as discussed in the class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Difference between Fixnum and Bignum in Ruby'''&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
Example 4:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example.&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in Ruby,assignments physically copy references, but not the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,the reference to the method upcase of the string is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol and thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
Here we compute the area under a curve say f(x) between two points say 'a' and 'b' by dividing the region under the curve into small rectangles with the lengths parallel to the y axis.The area of the rectangle can be obtained by multiplying the height and the breadth.Height is the average of the values of f(x) at the two vertices.Breadth is the distance between the two vertices. Sum of the areas of all the rectangles gives us an approximation of the area of the area under f(x).&lt;br /&gt;
&lt;br /&gt;
e.g.Suppose we need to compute the area under a curve described by some function,  x2 + 2x + 3 between say 10 and 20. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,end points between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the points x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
To do this, we pick an interval, say (20−10)/100 = 1/10, and evaluate the polynomial for values between 10 and 20 at intervals of 1/10, e.g., 10, 10.1, 10.2, … , 19.9, 20.0. We take the value of the polynomial at 2 successive points, average these two values(height of rectangle), and multiply by 1/10 (the distance between the two points ie the width).  After we do this for all 100 intervals and add up the areas, we obtain an approximation of the area under the curve.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility. This feature is not available in java as a class cast exception is thrown whenever an a call to an undefined method is made. Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
'''&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Roman &lt;br /&gt;
&lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    prev = nil #prev stores the previous value in roman string&lt;br /&gt;
    roman_string.to_s.upcase.split(//).reverse.inject(0) do |memo, digit|&lt;br /&gt;
      if digit_value = DIGITS[digit] #checks if the integer value is present in the hash&lt;br /&gt;
        if prev &amp;amp;&amp;amp; prev &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
        prev = digit_value&lt;br /&gt;
      end&lt;br /&gt;
      memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end    &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
 r=Roman.new&lt;br /&gt;
 r.XV&lt;br /&gt;
Output : =&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example we declare a class Roman containing a hash called DIGITS which maps  roman symbols with the corresponding integers. The class also contains the methods method_missing and roman_to_integer. When an object of Roman class( say r) is created and when we do r.XV ,method_missing(:XV) is  invoked since the Roman class has no method named XV. Inside method_missing, the symbol is converted to &amp;quot;XV&amp;quot;(string) and  roman_to_str(&amp;quot;XV&amp;quot;) is called. In roman_to_str method, the string is converted to uppercase,split into an array and the array is reversed. The inject operator  then passes  each element in the array  and an accumulator value (memo)to the block.The initial value of  memo  For each element,if the previous digit_value exists and is greater than the current digit_value,the  current digit_value is added to memo. Else current digit_value is subtracted from memo.The result becomes the new value for memo. At the end of the iteration, the final value of memo is the integer value of the roman string.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility.  Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.&lt;br /&gt;
&lt;br /&gt;
==Reflection in Java==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52738</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52738"/>
		<updated>2011-10-19T21:39:04Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Applications of passing methods as parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 4: Let us now consider the example as discussed in the class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difference between Fixnum and Bignum in Ruby===&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example 3:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
eg:&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in ruby,assignments physically copy references, but  not  the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,the reference to the method upcase of the string is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol and thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
&lt;br /&gt;
Suppose we need to compute the area under a curve described by some function, &lt;br /&gt;
[[File:Integral as region under curve.svg|thumb]]e.g., x2 + 2x + 3. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,upper bound and lower bound between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the limits x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility.  Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52737</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52737"/>
		<updated>2011-10-19T21:37:24Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Applications of passing methods as parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 4: Let us now consider the example as discussed in the class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difference between Fixnum and Bignum in Ruby===&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example 3:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
eg:&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in ruby,assignments physically copy references, but  not  the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,the reference to the method upcase of the string is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol and thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
&lt;br /&gt;
Suppose we need to compute the area under a curve described by some function, &lt;br /&gt;
[[File:Integral as region under curve.svg]thumb]e.g., x2 + 2x + 3. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,upper bound and lower bound between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the limits x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility.  Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52735</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52735"/>
		<updated>2011-10-19T21:10:18Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Obtaining Reference to a method in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 4: Let us now consider the example as discussed in the class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difference between Fixnum and Bignum in Ruby===&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example 3:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
eg:&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in ruby,assignments physically copy references, but  not  the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example,the reference to the method upcase of the string is obtained by passing the symbol :upcase to the 'method' method of the String class. The reference can then be used to invoke the method 'upcase' on the string object.&lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol and thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
&lt;br /&gt;
Suppose we need to compute the area under a curve described by some function, e.g., x2 + 2x + 3. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,upper bound and lower bound between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the limits x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility.  Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52734</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52734"/>
		<updated>2011-10-19T21:06:14Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Obtaining Reference to a method in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 4: Let us now consider the example as discussed in the class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difference between Fixnum and Bignum in Ruby===&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example 3:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
eg:&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in ruby,assignments physically copy references, but  not  the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol and thus symbols cannot appear in the left side of an assigment.In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
&lt;br /&gt;
Suppose we need to compute the area under a curve described by some function, e.g., x2 + 2x + 3. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,upper bound and lower bound between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the limits x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility.  Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52733</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52733"/>
		<updated>2011-10-19T21:04:04Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Difference between Fixnum and Bignum in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 4: Let us now consider the example as discussed in the class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difference between Fixnum and Bignum in Ruby===&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example 3:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
eg:&amp;lt;pre&amp;gt; if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in ruby,assignments physically copy references, but  not  the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol .In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
&lt;br /&gt;
Suppose we need to compute the area under a curve described by some function, e.g., x2 + 2x + 3. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,upper bound and lower bound between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the limits x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility.  Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52732</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52732"/>
		<updated>2011-10-19T21:02:52Z</updated>

		<summary type="html">&lt;p&gt;Svontel: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 4: Let us now consider the example as discussed in the class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difference between Fixnum and Bignum in Ruby===&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example 3:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
eg: if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in ruby,assignments physically copy references, but  not  the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol .In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
&lt;br /&gt;
Suppose we need to compute the area under a curve described by some function, e.g., x2 + 2x + 3. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,upper bound and lower bound between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the limits x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility.  Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52731</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52731"/>
		<updated>2011-10-19T21:00:44Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Examples of Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
eg: if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'. The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in ruby,assignments physically copy references, but  not  the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol .In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
&lt;br /&gt;
Suppose we need to compute the area under a curve described by some function, e.g., x2 + 2x + 3. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,upper bound and lower bound between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the limits x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility.  Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example 4: Let us now consider the example as discussed in the class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difference between Fixnum and Bignum in Ruby===&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52730</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52730"/>
		<updated>2011-10-19T20:59:31Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Obtaining Reference to a method in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
eg: if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
 Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'.The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in ruby,assignments physically copy references, but  not  the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call  #returns &amp;quot;HELLO,WORLD&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Note that in the above example name of the method passed as a symbol.This is because symbols are immutable and every instance of a particular symbol is the same symbol .In contrast,every instance of a particular  string is a different string and hence has a different object id as seen in the example below.&lt;br /&gt;
example :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown next)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
&lt;br /&gt;
Suppose we need to compute the area under a curve described by some function, e.g., x2 + 2x + 3. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,upper bound and lower bound between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the limits x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility.  Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example 4: Let us now consider the example as discussed in the class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difference between Fixnum and Bignum in Ruby===&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52729</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52729"/>
		<updated>2011-10-19T20:51:05Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Obtaining Reference to a method in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
eg: if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
 Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'.The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in ruby,assignments physically copy references, but  not  the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Note that in the above example name of the method passed as a symbol.Every instance of a particular symbol is the same symbol. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown below)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
&lt;br /&gt;
Suppose we need to compute the area under a curve described by some function, e.g., x2 + 2x + 3. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,upper bound and lower bound between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the limits x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility.  Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example 4: Let us now consider the example as discussed in the class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difference between Fixnum and Bignum in Ruby===&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52728</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52728"/>
		<updated>2011-10-19T20:50:06Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Examples of Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
eg: if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&lt;br /&gt;
'''Assignment''' &lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
 Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'.The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in ruby,assignments physically copy references, but  not  the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Note that in the above example name of the method passed as a symbol.Every instance of a particular symbol is the same symbol. Every instance of a particular symbol is the same symbol.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown below)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
&lt;br /&gt;
Suppose we need to compute the area under a curve described by some function, e.g., x2 + 2x + 3. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,upper bound and lower bound between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the limits x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility.  Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example 4: Let us now consider the example as discussed in the class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difference between Fixnum and Bignum in Ruby===&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52727</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52727"/>
		<updated>2011-10-19T20:48:27Z</updated>

		<summary type="html">&lt;p&gt;Svontel: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
eg: if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
 Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'.The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in ruby,assignments physically copy references, but  not  the objects.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Note that in the above example name of the method passed as a symbol.Every instance of a particular symbol is the same symbol. Every instance of a particular symbol is the same symbol.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown below)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
&lt;br /&gt;
Suppose we need to compute the area under a curve described by some function, e.g., x2 + 2x + 3. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,upper bound and lower bound between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the limits x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility.  Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example 4: Let us now consider the example as discussed in the class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difference between Fixnum and Bignum in Ruby===&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52724</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52724"/>
		<updated>2011-10-19T20:46:35Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Examples of Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the [http://en.wikipedia.org/wiki/Object_(computer_science) object]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance] structure&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Method_(computer_science) Methods] it contains.&lt;br /&gt;
* Number of [http://en.wikipedia.org/wiki/Parameter_%28computer_science%29 parameters] of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the [http://en.wikipedia.org/wiki/Attribute_(computer_science) attributes] of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In Ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
&lt;br /&gt;
===Examples of Reflection in Ruby===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&lt;br /&gt;
Example1:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 3:&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the String class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the String class based on the definition of the rocket method.&lt;br /&gt;
&lt;br /&gt;
Object is the superclass of all classes.Its methods are available to all classes unless explicitly overridden. It provides methods like to_s,eql? etc.&lt;br /&gt;
&lt;br /&gt;
Basic Object is the parent class of all classes in ruby including Object class. It is an explicit blank class.&lt;br /&gt;
&lt;br /&gt;
Kernel is a module is included by class Object(hence a mixin).  Its methods are available in all Ruby objects. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 Fixnum.ancestors #returns the ancestors of Fixnum.&lt;br /&gt;
=&amp;gt; [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Integer is the basic for Fixnum and Bignum which are two concrete classes that hold whole numbers in ruby.&lt;br /&gt;
&lt;br /&gt;
Numeric is a class which is  used to provide methods like abs,floor,ceil,modulo,unary +,unary - etc. ie methods which can be performed on numbers in general.&lt;br /&gt;
&lt;br /&gt;
Comparable which is  an ancestor of the Fixnum class is a mixin.It is used to provide the comparable functionality (operations like &amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;= etc)to the FixNum class based on the definition of the rocket method.  &lt;br /&gt;
&lt;br /&gt;
We have discussed the Object and BasicObject classes in the above example&lt;br /&gt;
&lt;br /&gt;
Note: It is useful to print out the name of a class while debugging.A class of an object should not be tested while debugging in objected oriented programming. It is advised to use polymorphism to replace code that tests the class of an object and performs certain things. &lt;br /&gt;
&lt;br /&gt;
eg: if s.kind_of? Integer then this_method else that_method end #this is not a good practice. #Should be replaced by polymorphism.&lt;br /&gt;
&lt;br /&gt;
In some languages like C, C++, or Ada, an assignment statement like x = y is interpreted as copying the contents of variable y into the variable x. It is generally required that x,y should be of the same type and size. But it is allowed in certain cases when the variables are of similar types.In C++, if the sizes are different say size of x is less than the size of y there will be a loss of data during assignment.(Slicing).&lt;br /&gt;
&lt;br /&gt;
 Ruby exhibits dynamic binding, not static binding ie the type of the object in a variable is determined at runtime but not at compile time.&lt;br /&gt;
Therefore in ruby, x = y can be 'interpreted as bind x to the same object that y is bound to'.The assignment operation is implemented by copying the reference stored in y into x.&lt;br /&gt;
&lt;br /&gt;
Thus in ruby,assignments physically copy references, but  not  the objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example 4: Let us now consider the example as discussed in the class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difference between Fixnum and Bignum in Ruby===&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
===Obtaining Reference to a method in Ruby===&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Note that in the above example name of the method passed as a symbol.Every instance of a particular symbol is the same symbol. Every instance of a particular symbol is the same symbol.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown below)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Applications of passing methods as parameters===&lt;br /&gt;
&lt;br /&gt;
'''Quadrature integration'''&lt;br /&gt;
&lt;br /&gt;
Suppose we need to compute the area under a curve described by some function, e.g., x2 + 2x + 3. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,upper bound and lower bound between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the limits x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
===Intercepting calls to undefined methods===&lt;br /&gt;
&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
Implementation of  the method method_missing within the class definition provides this facility.  Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
===Illustration of Reflection in Java through Examples===&lt;br /&gt;
&lt;br /&gt;
*'''Class'''&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Field'''&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Method'''&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
===Applications of Reflection in Java===&lt;br /&gt;
&lt;br /&gt;
Many sophisticated Java applications rely on reflection. Apart from the general benefits of reflection, listed below are a few which are specific to Java in general.&lt;br /&gt;
&lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*Method can be easily invoked methods by their names, it is widely used in web, when invoking getters and setters by property names mentioned on jsp/jsf pages.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
Reflection has many advantages some of which have been listed below:&lt;br /&gt;
&lt;br /&gt;
* '''Extensibility Features:''' An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
* '''Class Browsers and Visual Development Environments''' A class browser will able to enumerate the members of classes and visual development environments can benefit from making use of type information available in reflection to help the developer in writing correct code. &lt;br /&gt;
* '''Debugging:''' Debuggers and Test Tools Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Reflection==&lt;br /&gt;
&lt;br /&gt;
*'''Performance Overhead:''' Reflection involves types that are dynamically resolved due to which certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts. Hence, they should be avoided in sections of code which are called frequently in performance-sensitive applications. &lt;br /&gt;
&lt;br /&gt;
*'''Security Restrictions:''' Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet in Java. &lt;br /&gt;
&lt;br /&gt;
*'''Exposure of Internals:''' Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
*Compile-check features are lost. You can't be sure you're operating the right amount of parameters, their types, you even can't be sure the method you call exists.&lt;br /&gt;
&lt;br /&gt;
==MetaProgramming==&lt;br /&gt;
&lt;br /&gt;
The greek prefix [http://en.wikipedia.org/wiki/Meta 'Meta'] refers to knowledge about knowledge. [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52569</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52569"/>
		<updated>2011-10-19T04:21:00Z</updated>

		<summary type="html">&lt;p&gt;Svontel: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
&lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
&lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the object&lt;br /&gt;
* Inheritance structure&lt;br /&gt;
* Methods it contains.&lt;br /&gt;
* Number of parameters of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the attributes of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk],  [http://www.ruby-lang.org/en/Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object. &lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
In ruby, reflection is simpler because it is done using the language mechanisms ie .methods gives the list of methods. In contrast reflection as we shall see later is much more verbose because we need to use class names,method names,parameter names etc.&lt;br /&gt;
Examples of reflection in ruby :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3.1419.methods #gives the list of methods of the float number.&lt;br /&gt;
irb(main):005:0&amp;gt; 3.1419.methods&lt;br /&gt;
=&amp;gt; [:to_s, :coerce, :-@, :+, :-, :*, :/, :quo, :fdiv, :%, :modulo, :divmod, :**,&lt;br /&gt;
 :==, :===, :&amp;lt;=&amp;gt;, :&amp;gt;, :&amp;gt;=, :&amp;lt;, :&amp;lt;=, :eql?, :hash, :to_f, :abs, :magnitude, :zero&lt;br /&gt;
?, :to_i, :to_int, :floor, :ceil, :round, :truncate, :nan?, :infinite?, :finite?&lt;br /&gt;
, :numerator, :denominator, :to_r, :rationalize, :arg, :angle, :phase, :singleto&lt;br /&gt;
n_method_added, :i, :+@, :div, :remainder, :real?, :integer?, :nonzero?, :step,&lt;br /&gt;
:to_c, :real, :imaginary, :imag, :abs2, :rectangular, :rect, :polar, :conjugate,&lt;br /&gt;
 :conj, :between?, :nil?, :=~, :!~, :class, :singleton_class, :clone, :dup, :ini&lt;br /&gt;
tialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untruste&lt;br /&gt;
d?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protecte&lt;br /&gt;
d_methods, :private_methods, :public_methods, :instance_variables, :instance_var&lt;br /&gt;
iable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :&lt;br /&gt;
kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?,&lt;br /&gt;
 :extend, :display, :method, :public_method, :define_singleton_method, :__id__,&lt;br /&gt;
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exe&lt;br /&gt;
c, :__send__]&lt;br /&gt;
&amp;lt;/pre&amp;gt; This is an example of discovering things about 3.1459 at runtime.(Reflection).We can act on information during runtime without compiling it in. Information needed can be found out at run-time instead of writing code at compile time.&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MetaProgramming :&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The greek prefix 'Meta' refers to knowledge about knowledge. Metaprogramming means programs can create new code at runtime and run the code that is written. Program that writes a program.The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts 100.class # print the class of 1&lt;br /&gt;
&amp;gt;&amp;gt; Fixnum&lt;br /&gt;
puts 98343098452405890454.class #prints the class of 98343098452405890454&lt;br /&gt;
&amp;gt;&amp;gt; Bignum&lt;br /&gt;
puts 123456789012345.kind_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
puts 123456789012345.instance_of? Integer&lt;br /&gt;
&amp;gt;&amp;gt; false&lt;br /&gt;
puts 123456789012345.instance_of? Bignum&lt;br /&gt;
&amp;gt;&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Difference between Fixnum and Bignum in Ruby :&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Fixnum stores Integer values which can be represented in a native machine word (minus 1 bit). If an operation on a Fixnum results in a value that exceeds the range, the value is automatically converted to a Bignum.&lt;br /&gt;
&lt;br /&gt;
When Fixnum objects  are assigned or passed as parameters, the actual objects are passed, rather than  references to the objects. There is  only one Fixnum object instance for a given integer value&lt;br /&gt;
&lt;br /&gt;
Bignum objects are used to store values of integers outside the range of Fixnum. They are created automatically when encountered with integer calculations that would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.&lt;br /&gt;
&lt;br /&gt;
OO Languages distinguish between a primitive and reference with help of a  leading or trailing bit. ie One bit indicates if the remaining bits store a primitive or a reference and the remaining bits represent the value. In numeric  calculations, most of numbers are primitives so primitives can be used directly avoiding the overhead of indirection. One main difference between Fixnum and Bignum is that Fixnum can be stored in 1 machine word. If the object cannot be stored in a machine word then Bignum is used. In case of Bignum a reference to the actual object is stored.&lt;br /&gt;
&lt;br /&gt;
In contrast to Fixnum objects, in case of objects references to objects are used for parameter passing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Obtaining Reference to a method in ruby:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
A reference to a method can be obtained by  using the method ''method'' in the class ''Object''. &lt;br /&gt;
It is available to all classes, since Object is the superclass of all classes.  The reference thus obtained  can be used to invoke that particular method. &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str = &amp;quot;Hello, World&amp;quot;&lt;br /&gt;
#The name of the method has to be passed as a symbol to method:&lt;br /&gt;
m = str.method(:upcase) # returns a Method object which is  a closure.&lt;br /&gt;
puts m.call&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 Note that in the above example name of the method passed as a symbol.Every instance of a particular symbol is the same symbol. Every instance of a particular symbol is the same symbol.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):009:0&amp;gt; puts :mysymbol.object_id&lt;br /&gt;
332648&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):010:0&amp;gt; puts :mysymbol.object_id #Note that 2 occurrences of the same symbol yielded &lt;br /&gt;
332648                                    #same object id unlike strings (shown below)&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):011:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
21196488&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):012:0&amp;gt; puts &amp;quot;mystring&amp;quot;.object_id&lt;br /&gt;
20959140&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
application of passing methods as parameters :Quadrature integration&lt;br /&gt;
Suppose we need to compute the area under a curve described by some function, e.g., x2 + 2x + 3. &lt;br /&gt;
&lt;br /&gt;
Then we can define&lt;br /&gt;
class Float&lt;br /&gt;
  def poly&lt;br /&gt;
    self*self + 2*self + 3&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
And to compute the area,we  pass poly to an integration method which takes the method reference,upper bound and lower bound between which the area is to be computed as the parameters &lt;br /&gt;
area = integrate(:poly, 10, 20) #calculates the area of the curve x2+2x+3 between the limits x=10 to x=20.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
Intercepting calls to undefined methods'''&lt;br /&gt;
Ruby provides an option to intercept the call when a call to an undefined method is made on an object.&lt;br /&gt;
 Implementation of  the method method_missing within the class definition provides this facility.  Ruby passes as parameters the name of the method called and the arguments passed to it. &lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quack&amp;quot;&lt;br /&gt;
  end  &lt;br /&gt;
  def method_missing(meth, *args)&lt;br /&gt;
    puts &amp;quot;Sorry, I do not #{meth}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
d = Duck.new&lt;br /&gt;
d.quack&lt;br /&gt;
&amp;gt;&amp;gt;Quack&lt;br /&gt;
d.bark&lt;br /&gt;
&amp;gt;&amp;gt; Sorry, I do not bark &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example of an interesting usage of method_missing: “Roman method_missing”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
'''Illustration of Reflection in Java through Examples'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Field'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Method'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
'''Applications of Reflection in Java''':Many sophisticated Java applications rely on reflection &lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*It is used class browser which needs  to be able to enumerate the members of classes.&lt;br /&gt;
*It is also employed in  Visual development environments. The type information available in reflection is used to help the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Benefits'''&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible,extensible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable and easier to understand.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52492</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52492"/>
		<updated>2011-10-18T21:33:16Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
&lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
&lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the object&lt;br /&gt;
* Inheritance structure&lt;br /&gt;
* Methods it contains.&lt;br /&gt;
* Number of parameters of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the attributes of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk],  [http://www.ruby-lang.org/en/Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object. &lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
Examples of reflection in ruby :&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class  #returns the class of object &amp;quot;hello&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;String&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;puts &amp;quot;hello&amp;quot;.class.superclass  #returns the superclass of the String class&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;Object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;puts string.ancestors #returns the ancestors of the string class&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;String&lt;br /&gt;
Comparable&lt;br /&gt;
Object&lt;br /&gt;
Basic Object&lt;br /&gt;
Kernel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Obtaining Reference to a&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
'''Illustration of Reflection in Java through Examples'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Field'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Method'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
'''Applications of Reflection in Java''':Many sophisticated Java applications rely on reflection &lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*It is used class browser which needs  to be able to enumerate the members of classes.&lt;br /&gt;
*It is also employed in  Visual development environments. The type information available in reflection is used to help the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Benefits'''&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible,extensible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52465</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52465"/>
		<updated>2011-10-18T19:06:38Z</updated>

		<summary type="html">&lt;p&gt;Svontel: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
&lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
&lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
* The type of the object&lt;br /&gt;
* Inheritance structure&lt;br /&gt;
* Methods it contains.&lt;br /&gt;
* Number of parameters of the methods,types of parameters, return types.&lt;br /&gt;
* Names and types of the attributes of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk],  [http://www.ruby-lang.org/en/Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object. &lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflection in Java ==&lt;br /&gt;
Reflection in Java is much more verbose than in ruby.It is an advanced feature of the Java environment. Reflection is achieved using the Reflection API.The verbosity of reflection in java may be associated with the usage of the library for reflection.&lt;br /&gt;
&lt;br /&gt;
Big industrial-strength protocols like SOAP and JavaBeans wouldn't be possible if it weren't for Reflection. Every time you drag-and-drop an object in your favorite IDE into a form, Reflection is orchestrating the action behind the scenes. Actually, most sophisticated Java applications rely on Reflection in one way or another.&lt;br /&gt;
&lt;br /&gt;
Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. Some of the questions  that can be answered using reflection in java:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 *   Which class does an object belong to?&lt;br /&gt;
 *   What is the description of a given class name?&lt;br /&gt;
 *   What are the fields in a given class?&lt;br /&gt;
 *   What is the type of a field?&lt;br /&gt;
 *   What are the methods in a class?&lt;br /&gt;
 *   What are the parameters of a method?&lt;br /&gt;
 *   What are the constructors of a given class? &lt;br /&gt;
 *   Constructing an object using a given constructor&lt;br /&gt;
 *   Invoking an object's method using such-and-such parameters&lt;br /&gt;
 *   Assigning a value to an object's field&lt;br /&gt;
 *   Dynamically creating and manipulating arrays  &lt;br /&gt;
&lt;br /&gt;
'''Java Reflection Classes'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All Reflection classes (With the exception of the class Class that resides in the default Java package) are contained in the package java.lang.reflect.&lt;br /&gt;
&lt;br /&gt;
Following are some of the classes used to various represent members of a class.:&lt;br /&gt;
Classes-'Class'&lt;br /&gt;
class Fields-'Field' class&lt;br /&gt;
methods-'Method' class&lt;br /&gt;
constructors-'Constructor' class&lt;br /&gt;
arrays-'Array' class.&lt;br /&gt;
&lt;br /&gt;
'''Illustration of Reflection in Java through Examples'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Each class or interface in Java is described by a Class object.It contains methods to obtain  details about the class like name, parent class, constructors, fields, methods, interfaces implemented etc.&lt;br /&gt;
&lt;br /&gt;
To obtain the class that an object belongs to, invoke the method Class getClass()(returns 'Class') of the  'Object' class (superclass of all the Java classes hierarchy)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
Class theClass = myStr.getClass(); // 'theClass' now contains 'String'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The property &amp;quot;.class&amp;quot;(available for every java class) returns a Class object for the class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String myStr = &amp;quot;HelloWorld&amp;quot;;&lt;br /&gt;
if (myStr.getClass()==String.class)&lt;br /&gt;
System.out.println(&amp;quot;The object is a String&amp;quot;); // prints &amp;quot;The object is a String&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The wrapper classes (Integer, Boolean, Double,...) contain a &amp;quot;.TYPE&amp;quot; property that returns the Class object representing the primitive type. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class myClass = Integer.TYPE; //&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Field'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Field class describes the different attributes of a Java class field. Information such as  a field name, its type, and its accessibility can be obtained from a field object. It also contains methods to set and get the field's value for a given object &lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class MyfieldClass {&lt;br /&gt;
      int i = 100;&lt;br /&gt;
      String s = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class cls = Class.forName(&amp;quot;MyfieldClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Field flist[]= cls.getDeclaredFields();&lt;br /&gt;
            for (int i = 0; i &amp;lt; flist.length; i++) {&lt;br /&gt;
              &lt;br /&gt;
               Field f = flist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name&lt;br /&gt;
                  = &amp;quot; + f.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +&lt;br /&gt;
                           f.getDeclaringClass());&lt;br /&gt;
               System.out.println(&amp;quot;type&lt;br /&gt;
                  = &amp;quot; + f.getType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 The output of the program is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = i&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = double&lt;br /&gt;
   &lt;br /&gt;
   name = s&lt;br /&gt;
   decl class = MyfieldClass&lt;br /&gt;
   type = class java.lang.String&lt;br /&gt;
   &lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Method'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Method class is used to obtain information(the method name, its type, its accessibility, and its parameter types) about class methods.We can even invoke the method on a particular object and pass a set of parameters to it. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
   public class myMethodClass {&lt;br /&gt;
      private int myMethod(int y, int x) &lt;br /&gt;
      {&lt;br /&gt;
         return x+y;&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class cls = Class.forName(&amp;quot;myMethodClass&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
            Method mlist[] = cls.getDeclaredMethods();&lt;br /&gt;
            &lt;br /&gt;
              for (int i = 0; i &amp;lt; mlist.length;i++) &lt;br /&gt;
              {  &lt;br /&gt;
               Method m = mlist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + m.getName());&lt;br /&gt;
               System.out.println(&amp;quot;decl class = &amp;quot; +m.getDeclaringClass());&lt;br /&gt;
                              &lt;br /&gt;
               Class pt[] = m.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt; pt.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; + j + &amp;quot; &amp;quot; + pt[j]);&lt;br /&gt;
                   &lt;br /&gt;
              &lt;br /&gt;
               System.out.println(&amp;quot;return type = &amp;quot; +&lt;br /&gt;
                                  m.getReturnType());&lt;br /&gt;
               &lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the program is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  name = myMethod&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 int&lt;br /&gt;
   param #1 int&lt;br /&gt;
   &lt;br /&gt;
   return type = int&lt;br /&gt;
   &lt;br /&gt;
   name = main&lt;br /&gt;
   decl class = class myMethodClass&lt;br /&gt;
   param #0 class [Ljava.lang.String;&lt;br /&gt;
   return type = void&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Constructor'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Constructor class can be used  to obtain information(parameter types, number of parameters, and accessibility) about class constructors.We can also invoke the constructor to create new object instances &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import java.lang.reflect.*;&lt;br /&gt;
        &lt;br /&gt;
   public class Myconstructor {&lt;br /&gt;
      public Myconstructor()&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
        &lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
           Class c = Class.forName(&amp;quot;Myconstructor&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
           Constructor clist[]= c.getDeclaredConstructors();&lt;br /&gt;
         &lt;br /&gt;
           for (int i = 0; i &amp;lt; clist.length; i++) {&lt;br /&gt;
               Constructor ct = clist[i];&lt;br /&gt;
               System.out.println(&amp;quot;name= &amp;quot; + ct.getName());&lt;br /&gt;
               System.out.println(&amp;quot;Declaring Class Name = &amp;quot; +&lt;br /&gt;
                            ct.getDeclaringClass());&lt;br /&gt;
               Class pTypes[] = ct.getParameterTypes();&lt;br /&gt;
               for (int j = 0; j &amp;lt;  pTypes.length; j++)&lt;br /&gt;
                  System.out.println(&amp;quot;param #&amp;quot; &lt;br /&gt;
                     + j + &amp;quot; &amp;quot; +  pTypes[j]);&lt;br /&gt;
               &lt;br /&gt;
              &lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          catch (Throwable e) {&lt;br /&gt;
             System.err.println(e);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name=MyConstructor&lt;br /&gt;
Declaring Class name=MyConstructor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
'''Applications of Reflection in Java''':Many sophisticated Java applications rely on reflection &lt;br /&gt;
* Reflection is used extensively in protocols like  [http://www.w3schools.com/soap/soap_intro.asp SOAP]&lt;br /&gt;
*Whenever we  drag-and-drop an object in an  [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] to use it in a form, Reflection is used behind the scenes.&lt;br /&gt;
*Java Beans&lt;br /&gt;
*It is used in Debuggers and Test Tools which require examining the private members &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
*It is used class browser which needs  to be able to enumerate the members of classes.&lt;br /&gt;
*It is also employed in  Visual development environments. The type information available in reflection is used to help the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Benefits'''&lt;br /&gt;
* Reflection helps in keeping software  [http://www.linfo.org/robust.html robust]&lt;br /&gt;
* It helps in making the applications more flexible,extensible and pluggable.&lt;br /&gt;
* It helps in making the source code more readable.&lt;br /&gt;
* It can simplify source code and design.&lt;br /&gt;
* Expensive conditional code can be reduced/removed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
See more:&lt;br /&gt;
http://download.oracle.com/javase/tutorial/reflect/&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52433</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_ss&amp;diff=52433"/>
		<updated>2011-10-18T14:13:23Z</updated>

		<summary type="html">&lt;p&gt;Svontel: Created page with &amp;quot;== Reflection ==   Reflection allows program entities to discover things about themselves through introspection.    In other words,Reflection is the ability of a program to deter...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows program entities to discover things about themselves through introspection.  &lt;br /&gt;
&lt;br /&gt;
In other words,Reflection is the ability of a program to determine information about an object at runtime. &lt;br /&gt;
&lt;br /&gt;
The information may include the following :&lt;br /&gt;
&lt;br /&gt;
*)The type of the object&lt;br /&gt;
*)Inheritance structure&lt;br /&gt;
*)Methods it contains.&lt;br /&gt;
*)Number of parameters of the methods,types of parameters, return types.&lt;br /&gt;
*) Names and types of the attributes of the object.&lt;br /&gt;
&lt;br /&gt;
Reflection is supported by many  [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming]languages in various forms. Powerful and flexible reflection mechanisms are exhibited by  [http://www.smalltalk.org/main/ Smalltalk],  [http://www.ruby-lang.org/en/Ruby], and [http://www.python.org/ Python] .  [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] also supports reflection. But reflection in java is verbose and is not as flexible and dynamic as these languages. [http://www.cplusplus.com/doc/tutorial/ C++] allows a program to determine the type of an object at run-time. Thus it does support reflection but in  a limited form only. [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] also has support for a limited form of reflection which  includes the ability to determine the features contained in an object. &lt;br /&gt;
&lt;br /&gt;
== Reflection in Ruby ==&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=52412</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=52412"/>
		<updated>2011-10-18T05:04:33Z</updated>

		<summary type="html">&lt;p&gt;Svontel: &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 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;
&lt;br /&gt;
*[[trial]]&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51649</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51649"/>
		<updated>2011-10-01T03:57:28Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Singleton Behavior */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
&lt;br /&gt;
Example of Comparable behavior: Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the 'each' method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''  Mix in Enumerable in a class as follows'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s 'each' method yields each element.The Hash class‘s 'each' yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
myrecipee.save #To insert into the DB&lt;br /&gt;
&lt;br /&gt;
myrecipee.Description=’Tasty and Delicious!’ #to modify the DB&lt;br /&gt;
 &lt;br /&gt;
myrecipee.destroy #to delete the tuple in the database&lt;br /&gt;
&lt;br /&gt;
puts myrecipee.inspect &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private and providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A  [http://javapapers.com/core-java/explain-the-java-static-modifier/  static]modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:''' In the following example,We create a class called 'SingletonClass' . In this class,we declare a private static instance variable called 'singletonObject'.The constructor of the class is also made private.A public static accessor method ,'getSingletonObject' is defined which returns the reference to the instance of the class that is stored in the private instance variable singletonObject. Note that the object is created only the first time the accessor is called. Other invocations of the method yield the same object.  Also the 'clone' method of the Object class is overridden to disallow cloning. The class 'SingletonObjectDemo' gets an instance of SingletonClass by using the public accessor method.An attempt to create an object of SingletonClass using 'new' throws compilation error as the constructor is made private.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51648</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51648"/>
		<updated>2011-10-01T03:44:00Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Singleton Behavior */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
&lt;br /&gt;
Example of Comparable behavior: Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the 'each' method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''  Mix in Enumerable in a class as follows'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s 'each' method yields each element.The Hash class‘s 'each' yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
myrecipee.save #To insert into the DB&lt;br /&gt;
&lt;br /&gt;
myrecipee.Description=’Tasty and Delicious!’ #to modify the DB&lt;br /&gt;
 &lt;br /&gt;
myrecipee.destroy #to delete the tuple in the database&lt;br /&gt;
&lt;br /&gt;
puts myrecipee.inspect &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private and providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A  [http://javapapers.com/core-java/explain-the-java-static-modifier/  static]modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:''' In the following example,We create a class called 'SingletonClass' .In the class,we declare a private static instance variable called 'singletonObject'.The constructor of the class is also made private.A public static accessor method ,'getSingletonObject' is defined which returns the reference to the instance of the class stored in the private instance variable singletonObject. Note that the object is created only the first time the accessor is called. Other invocations of the method yield the same object.  Also the 'clone' method of the Object class is overridden to disallow cloning. The class 'SingletonObjectDemo' gets an instance of SingletonClass by using the public accessor method.An attempt to create an object of SingletonClass using 'new' throws compilation error as the constructor is made private.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51647</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51647"/>
		<updated>2011-10-01T03:37:59Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Singleton Behavior */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
&lt;br /&gt;
Example of Comparable behavior: Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the 'each' method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''  Mix in Enumerable in a class as follows'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s 'each' method yields each element.The Hash class‘s 'each' yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
myrecipee.save #To insert into the DB&lt;br /&gt;
&lt;br /&gt;
myrecipee.Description=’Tasty and Delicious!’ #to modify the DB&lt;br /&gt;
 &lt;br /&gt;
myrecipee.destroy #to delete the tuple in the database&lt;br /&gt;
&lt;br /&gt;
puts myrecipee.inspect &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private and providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:''' In the following example,We create a class called 'SingletonClass' .In the class,we declare a private static instance variable called 'singletonObject'.The constructor of the class is also made private.A public static accessor method ,'getSingletonObject' is defined which returns the reference to the instance of the class stored in the private instance variable singletonObject. Note that the object is created only the first time the accessor is called. Other invocations of the method yield the same object.  Also the 'clone' method of the Object class is overridden to disallow cloning. The class 'SingletonObjectDemo' gets an instance of SingletonClass by using the public accessor method.An attempt to create an object of SingletonClass using 'new' throws compilation error as the constructor is made private.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51646</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51646"/>
		<updated>2011-10-01T03:12:19Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* References: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
&lt;br /&gt;
Example of Comparable behavior: Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the 'each' method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''  Mix in Enumerable in a class as follows'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s 'each' method yields each element.The Hash class‘s 'each' yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
myrecipee.save #To insert into the DB&lt;br /&gt;
&lt;br /&gt;
myrecipee.Description=’Tasty and Delicious!’ #to modify the DB&lt;br /&gt;
 &lt;br /&gt;
myrecipee.destroy #to delete the tuple in the database&lt;br /&gt;
&lt;br /&gt;
puts myrecipee.inspect &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private and providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51645</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51645"/>
		<updated>2011-10-01T03:11:06Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Comparable Behavior */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
&lt;br /&gt;
Example of Comparable behavior: Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the 'each' method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''  Mix in Enumerable in a class as follows'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s 'each' method yields each element.The Hash class‘s 'each' yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
myrecipee.save #To insert into the DB&lt;br /&gt;
&lt;br /&gt;
myrecipee.Description=’Tasty and Delicious!’ #to modify the DB&lt;br /&gt;
 &lt;br /&gt;
myrecipee.destroy #to delete the tuple in the database&lt;br /&gt;
&lt;br /&gt;
puts myrecipee.inspect &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private and providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51644</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51644"/>
		<updated>2011-10-01T03:08:17Z</updated>

		<summary type="html">&lt;p&gt;Svontel: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
&lt;br /&gt;
Example of Comparable behavior: Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example[[Example of Comparable behavior:]].The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the 'each' method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''  Mix in Enumerable in a class as follows'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s 'each' method yields each element.The Hash class‘s 'each' yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
myrecipee.save #To insert into the DB&lt;br /&gt;
&lt;br /&gt;
myrecipee.Description=’Tasty and Delicious!’ #to modify the DB&lt;br /&gt;
 &lt;br /&gt;
myrecipee.destroy #to delete the tuple in the database&lt;br /&gt;
&lt;br /&gt;
puts myrecipee.inspect &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private and providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51643</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51643"/>
		<updated>2011-10-01T02:53:54Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Singleton Behavior */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the 'each' method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''  Mix in Enumerable in a class as follows'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s 'each' method yields each element.The Hash class‘s 'each' yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
myrecipee.save #To insert into the DB&lt;br /&gt;
&lt;br /&gt;
myrecipee.Description=’Tasty and Delicious!’ #to modify the DB&lt;br /&gt;
 &lt;br /&gt;
myrecipee.destroy #to delete the tuple in the database&lt;br /&gt;
&lt;br /&gt;
puts myrecipee.inspect &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private and providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51642</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51642"/>
		<updated>2011-10-01T02:35:11Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Data Mapper */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the 'each' method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''  Mix in Enumerable in a class as follows'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s 'each' method yields each element.The Hash class‘s 'each' yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
myrecipee.save #To insert into the DB&lt;br /&gt;
&lt;br /&gt;
myrecipee.Description=’Tasty and Delicious!’ #to modify the DB&lt;br /&gt;
 &lt;br /&gt;
myrecipee.destroy #to delete the tuple in the database&lt;br /&gt;
&lt;br /&gt;
puts myrecipee.inspect &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51641</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51641"/>
		<updated>2011-10-01T01:45:15Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Enumerable Behavior */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the 'each' method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''  Mix in Enumerable in a class as follows'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s 'each' method yields each element.The Hash class‘s 'each' yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51640</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51640"/>
		<updated>2011-10-01T01:42:46Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Enumerable Behavior */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the 'each' method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''  Mix in Enumerable in a class as follows'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s 'each' method yields each element.The Hash class‘s 'each' yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
 For example, to print all elements of a vector v:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51639</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51639"/>
		<updated>2011-10-01T01:39:52Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Enumerable Behavior */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the 'each' method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''  Mix in Enumerable in a class as follows'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s 'each' method yields each element.The Hash class‘s 'each' yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51638</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51638"/>
		<updated>2011-10-01T01:38:01Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Enumerable Behavior */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the 'each' method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s 'each' method yields each element.The Hash class‘s 'each' yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51637</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51637"/>
		<updated>2011-10-01T01:25:48Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Comparable Behavior */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s each method yields each element.The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51636</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51636"/>
		<updated>2011-10-01T01:24:05Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Comparable Behavior */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s each method yields each element.The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51634</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51634"/>
		<updated>2011-10-01T00:47:55Z</updated>

		<summary type="html">&lt;p&gt;Svontel: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java, modules and   mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a requirement to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of our class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in there to compare the two objects of that class. &lt;br /&gt;
Consider the same example as above and the implementation for java is provided below.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s each method yields each element.The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51633</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51633"/>
		<updated>2011-10-01T00:45:20Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Some Pre-defined mixin modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java,  mixins in  Ruby and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a requirement to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of our class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in there to compare the two objects of that class. &lt;br /&gt;
Consider the same example as above and the implementation for java is provided below.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s each method yields each element.The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51628</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51628"/>
		<updated>2011-10-01T00:18:23Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Some Pre-defined mixin modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java,  mixins in  Ruby and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a requirement to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of our class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in there to compare the two objects of that class. &lt;br /&gt;
Consider the same example as above and the implementation for java is provided below.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s each method yields each element.The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51627</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51627"/>
		<updated>2011-10-01T00:15:58Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Some Pre-defined mixin modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java,  mixins in  Ruby and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace], Preci-sion, [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a requirement to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of our class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in there to compare the two objects of that class. &lt;br /&gt;
Consider the same example as above and the implementation for java is provided below.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s each method yields each element.The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51626</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51626"/>
		<updated>2011-10-01T00:14:43Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java,  mixins in  Ruby and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace], Preci-sion, [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator. &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a requirement to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of our class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in there to compare the two objects of that class. &lt;br /&gt;
Consider the same example as above and the implementation for java is provided below.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s each method yields each element.The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51624</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51624"/>
		<updated>2011-10-01T00:12:48Z</updated>

		<summary type="html">&lt;p&gt;Svontel: /* Modules in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java,  mixins in  Ruby and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module  has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
 We now define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However,  to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes &amp;lt;module name&amp;gt;' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace], Preci-sion, [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. &lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator. &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a requirement to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of our class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in there to compare the two objects of that class. &lt;br /&gt;
Consider the same example as above and the implementation for java is provided below.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
eg: the Array class ‘s each method yields each element.The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.&lt;br /&gt;
Following are the steps involved in using the DataMapper&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=&amp;gt; true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An instance  is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].&lt;br /&gt;
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class. &lt;br /&gt;
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.&lt;br /&gt;
&lt;br /&gt;
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.&lt;br /&gt;
&lt;br /&gt;
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the &amp;lt;=&amp;gt; method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a  exception when the methods like &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help  programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Svontel</name></author>
	</entry>
</feed>